gpt4 book ai didi

java - 由于某种奇怪的原因,方法被调用两次?

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:34 24 4
gpt4 key购买 nike

出于某种原因,在我添加名为“Oliver”的宠物后,主菜单会打印两次,并附带“无效选择”行。我只需要另一双眼睛来观察它,因为我已经连续观察了几个小时,并且一直在修复小错误,但没有成功。

运行时的代码如下所示:

     /*Welcome to the pet store.Type the letter to make your selection
A. List the pets in the store.
B. Age up the pets
C. Add a new pet
D. Adopt a pet
E. Quit
C
Please type in a name
Oliver
Please type in an age
22
Oliver has just been added to the store!
Welcome to the pet store.Type the letter to make your selection
A. List the pets in the store.
B. Age up the pets
C. Add a new pet
D. Adopt a pet
E. Quit
Invalid choice
Welcome to the pet store.Type the letter to make your selection
A. List the pets in the store.
B. Age up the pets
C. Add a new pet
D. Adopt a pet
E. Quit*/

这是我的主类代码:

    private static void mainmenu(){
System.out.println("Welcome to the pet store.Type the letter to make
your selection");
System.out.println("A."+" " + "List the pets in the store.");
System.out.println("B."+" " + "Age up the pets");
System.out.println("C."+" " + "Add a new pet");
System.out.println("D."+" " + "Adopt a pet");
System.out.println("E."+" " + "Quit");

MainPets.Getuserinput();

}

public static String Getuserinput(){

userinput=scan.nextLine();

return userinput;

}

public static void main (String [] args){
int pet3age;
String pet3name;
Pet Pet1=new Pet("Fido",3);
Pet Pet2=new Pet("Furball",1);
Pet Pet3=null;
int userinputint;

MainPets.mainmenu();


while(userinput.equals("A")||userinput.equals("B")||userinput.equals("C")||userinput.equals("D")||userinput.equals("E")){

switch(userinput){
case "C":

if (Pet3!=null&&userinput.equals("C")){
System.out.println("Sorry the store is full");
}

if(Pet3==null){
System.out.println("Please type in a name");
pet3name=scan.nextLine();
System.out.println("Please type in an age");
pet3age=scan.nextInt();
Pet3=new Pet(pet3name,pet3age);
System.out.println(pet3name + " has just been added to the store!");
}
MainPets.mainmenu();
break;
}
}
while(!userinput.equals("A")||!userinput.equals("B")||!userinput.equals("C")||!userinput.equals("D")||!userinput.equals("E")){
System.out.println("Invalid choice");
MainPets.mainmenu();
}

这是包含所有方法的类:

public class Pet {
String Name;
String AdoptionStatus;
int Age;

public Pet() {}

public Pet(String Name, int Age) {
this.Name = Name;
this.Age = Age;
}

public void SetName(String namesetup) {
Name = namesetup;
}

public String GetName() {
return Name;
}

public int GetAge() {
return Age;
}

public int ageincrease() {
return Age++;
}

public String Getadoptionstatus() {
return AdoptionStatus;
}

public void Setadoptionstatustonotadopted(int petnumber) {
AdoptionStatus="not adopted";
}

public void Setadoptionstatustoadopted(int petnumber){
AdoptionStatus="adopted";
}

}

最佳答案

看起来您正在尝试尽可能多地使用 static 来练习它的作用?

无论如何,请参阅下面的一个最小示例,您可以在此基础上进行构建(即,您可以根据需要“添加”新宠物多次输入“C”)。

static String petname, petage;
public static void main(String[] args) {
initialText();
String userinput = userInput();
while (userinput.equals("A") || userinput.equals("B") || userinput.equals("C") || userinput.equals("D") || userinput.equals("E")) {
if(userinput.equals("C")){
System.out.println("Please type in a name");
petname = userInput();
System.out.println("Please type in an age");
petage = userInput();
Pet p = new Pet(petname, petage);
System.out.println(petname + " has been added to the store.");
}
else{
System.out.println("Option not configured yet");
//TODO - the rest of the options
}
initialText();
userinput = userInput();
}
}

public static void initialText() {
System.out.println("Welcome to the pet store.Type the letter to make your selection");
System.out.println("A." + " " + "List the pets in the store.");
System.out.println("B." + " " + "Age up the pets");
System.out.println("C." + " " + "Add a new pet");
System.out.println("D." + " " + "Adopt a pet");
System.out.println("E." + " " + "Quit");
}

public static String userInput(){
Scanner s = new Scanner(System.in);
return s.nextLine();
}

它绝不是完美的,只是很快地把它拼凑在一起,给你一个工作的机会。

关于java - 由于某种奇怪的原因,方法被调用两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46647003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com