因此,我设置了一种方法,每次调用时将宠物的年龄增加1,但由于某种原因,它给了我最初设定的年龄。
这是列出我的方法和类的文件: 这是这样,但现在遵循代码块:
public class Pet
{
String Name;
int Age;
String AdoptionStatus;
String True="not adopted";
public Pet(){
}
public Pet(String Name,int Age){
this.Name=Name;
this.Age=Age;
}
public void SetName(String namesetup){
namesetup=Name;
}
public String GetName(){
return Name;
}
public int GetAge(){
return Age;
}
public int ageincrease(){
return Age+1;
}
public String Getadoptionstatus(){
return AdoptionStatus;
}
public void Setadoptionstatus(String setadoption2){
AdoptionStatus=True;
}
}
这是调用ageincrease()的另一个类,但最终得到零: 公共类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);
Pet Pet3=null;
System.out.println("Welcome to the pet store.Type the letter to make your selection");
MainPets.mainmenu();
while (Userinput.equals("E")||Userinput.equals("A")||Userinput.equals("B")||Userinput.equals("C")||Userinput.equals("D")){
if (Userinput.equals("E")){
System.out.println("Have a good day!");
break;
}
else if(Userinput.equals("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();
}
if(Userinput.equals("B")){
System.out.println("Everyone just got a little older.");
Pet1.ageincrease();//Still keeps Pet1 age to 3
Pet2.ageincrease();//Still keeps Pet2 age to 1
Userinput=scan.nextLine();
}
else if (Userinput.equals("C")){
System.out.println("Please type in a name");
Pet3name=scan.nextLine();
System.out.println("Please type in an age");
Pet3age=scan.nextInt();
Userinput=scan.nextLine();
}
}
}
}
答案 0 :(得分:0)
我为你清理了一些代码。
首先回答你的问题:你回来了宠物的年龄加1,而不是实际上为宠物的年龄赋予了新的价值。为此,请使用Age++
或Age = Age + 1
或Age += 1
。这应该为宠物的年龄分配一个新值,从而解决您的问题。
以下是我所做的更改:
SetName(String namesetup)
。您有了namesetup=Name
的语句,它将类的Name字段分配给方法的实例变量namesetup。您希望将传递给方法的内容分配给由Name=namesetup
ageincrease()
中的return语句更改为return Age++;
而不是return Age + 1;
。这与Age = Age + 1;
while
语句和switch
循环替换主类中的长while
循环。这很方便,因为您不需要在while循环中进行长条件检查。我使用了一个标志变量,用于在用户输入"E"
时进行标记。如果您不熟悉switch
语句,则可以对其进行阅读here。注意事项:
Userinput.equals("D")
,但是在while循环中没有任何东西可以处理该条件,这样就有可能产生无限循环。这是您编辑的宠物类:
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;
}
}
以下是我用以下内容替换你的while循环的内容:
int flag = 0;
while(flag != -1) {
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();
break;
case "B":
System.out.println("Everyone just got a little older.");
Pet1.ageincrease();//Still keeps Pet1 age to 3
Pet2.ageincrease();//Still keeps Pet2 age to 1
System.out.println(Pet1.GetAge() + " " + Pet2.GetAge());
Userinput=scan.nextLine();
break;
case "C":
System.out.println("Please type in a name");
Pet3name=scan.nextLine();
System.out.println("Please type in an age");
Pet3age=scan.nextInt();
Userinput=scan.nextLine();
break;
case "D":
//Not sure how you want to implement this
break;
case "E":
flag = -1;
break;
}
}