2017-10-09 91 views
1

出於某種原因,在添加一個名爲「Oliver」的寵物後,主菜單將打印出兩次「無效選擇」行。我只是需要另一雙眼睛來看它,因爲我一直在看它幾個小時,並一直在修正一些小錯誤,無濟於事。由於某種奇怪的原因被調用兩次?

代碼時RAN看起來是這樣的:

 /*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"; 
} 

} 
+4

請閱讀關於Java命名conventins。它幾乎覺得你故意違反了他們。 Serioulsy:你寫代碼的方式讓我轉身去做其他的事情...... – GhostCat

+0

[Scanner在使用next(),nextInt()或其他nextFoo()之後跳過nextLine()?](https:/ /stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) –

+1

你沒有使用開關嗎?如果你首先仔細檢查編譯錯誤... –

回答

0

它看起來像你試圖儘可能多地使用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(); 
    } 

這絕不是完美的,只是很快將它敲在一起,給你一個工作的機會。

+0

注意:我在'Pet'類中有'age'作爲字符串,但是這很容易改變。 – notyou

相關問題