所以我的代码出现了问题,我的循环在我的“C”情况之后结束了。我需要它打印出一条消息,说明商店已满,并保持循环备份并打印出主菜单。此外,当我在添加新宠物后列出所有宠物时,我的Pet3没有被保存。我的
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;
}
}
以下是我的Pet类的代码:
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 :(得分:0)
您的循环在Pet3!=null
时正在退出,因为您之后没有从扫描仪接收任何输入。
在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;