gpt4 book ai didi

java - 循环突然结束并且Java不保存对象?

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:53 24 4
gpt4 key购买 nike

所以我的代码有问题,我的循环在“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;
}

}

最佳答案

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;

关于java - 循环突然结束并且Java不保存对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46628339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com