2017-10-08 1 views
1

So habe ich ein Problem mit meinem Code, wo meine Schleife endet nach meinem "C" -Fall. Ich brauche es, um eine Nachricht auszudrucken, die sagt, dass der Laden voll ist, und halte die Schleife hoch und drucke das Hauptmenü aus. Außerdem wird mein Pet3 nicht gespeichert, wenn ich alle Tiere aufliste, nachdem ich ein neues hinzugefügt habe. MeinSchleife endet abrupt und Java speichert Objekt nicht?

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

Hier ist der Code für meine Hundeklasse:

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

}  

Antwort

0

Ihre Schleife extting wird, wenn Pet3!=null, weil Sie keine Eingabe von dem Scanner nach der Einnahme werden.

Nehmen Eingang in beiden Bedingungen innerhalb 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;