2017-10-08 178 views
1

所以我遇到了我的代碼問題,我的循環結束後我的「C」的情況。我需要它打印出一條消息,說明商店已滿並保持循環備份並打印出主菜單。另外,當我在添加新寵物後列出所有寵物時,我的寵物3沒有被保存。我循環結束突然和Java不保存對象?

import java.util.*; 
public class MainPets 
{ 

static Scanner scan = new Scanner(System.in); 

private static String Userinput; 

private static void mainmenu(){ 
    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"); 


    Userinput=scan.nextLine(); 
} 

public static String Getuserinput(){ 

    return Userinput; 
} 




public static void main (String [] args){ 
    int Pet3age; 
    String Pet3name; 
    Pet Pet1=new Pet("Fido",3); 
    Pet Pet2=new Pet("furball",1); 
    int Userinputint; 
    Pet Pet3=null; 


    System.out.println("Welcome to the pet store.Type the letter to make your selection"); 
    MainPets.mainmenu(); 




    while(Userinput.equals("A")||Userinput.equals("B")||Userinput.equals("C")||Userinput.equals("D")||Userinput.equals("E")){ 
    switch(Userinput) { 
     case "A": 
      System.out.println("Fido is "+Pet1.GetAge()+ " years old and is " + Pet1.Getadoptionstatus()); 
      System.out.println("furball is " + Pet2.GetAge()+ " years old and is " + Pet2.Getadoptionstatus()); 
      Userinput=scan.nextLine(); 

     case "B": 
      System.out.println("Everyone just got a little older."); 
      Pet1.ageincrease(); 
      Pet2.ageincrease(); 
      Userinput=scan.nextLine(); 

     case "C": 

      if (Pet3!=null){ 
      System.out.println("Sorry the store is full"); 
      Userinput=scan.nextLine(); 
      }/* If the Pet 3 spot has been filled I want it to print this 
      and loop back up to print the main menu again.*/ 

      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);/*This line is Not saving Pet3 as 
      a "Pet" class and when I try to list all the pets by pressing 
      A when it loops back up , Pet3 does not show up as a Pet*/ 


      Userinput=scan.nextLine();/* This is where my program just 
      ends.It doesn't even take a user input */ 




     } 



     case "D": 
      //will add later on 
      break; 
     case "E": 
      //will add later on 
      break; 
     } 
} 

這裏是代碼爲我的寵物類:當Pet3!=null,因爲你沒有後採取從掃描儀的任何輸入

public class Pet { 
    String Name, AdoptionStatus, True = "not adopted"; 
    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 Setadoptionstatus(String setadoption2) { 
    AdoptionStatus = True; 
} 

}  

回答

0

你的循環extting。裏面case "C":

if (Pet3!=null){ 
     System.out.println("Sorry the store is full"); 

} else { 
     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);/*This line is Not saving Pet3 as 
     a "Pet" class and when I try to list all the pets by pressing 
     A when it loops back up , Pet3 does not show up as a Pet*/ 

} 
Userinput=scan.nextLine(); //make sure to keep this line outside the curly braces. 
break; 
在這兩個條件

接受輸入