2017-10-08 3 views
1

Donc, j'ai un problème avec mon code où ma boucle se termine après mon cas "C". J'en ai besoin pour imprimer un message disant que le magasin est plein et garder la boucle de retour et imprimer le menu principal. De plus, mon Pet3 n'est pas sauvegardé lorsque je liste tous les animaux après en avoir ajouté un nouveau. MonBoucle se terminant abruptement et Java ne pas enregistrer l'objet?

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; 
     } 
} 

Voici le code de ma classe Animal familier:

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; 
} 

}  

Répondre

0

Votre boucle est extting quand Pet3!=null parce que vous ne prenez pas une entrée du scanner après.

Prenez entrée dans les deux conditions à l'intérieur 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;