【问题标题】:Method not working when called upon?调用时方法不起作用?
【发布时间】:2017-10-07 22:40:17
【问题描述】:

所以我设置了一个方法,每次调用宠物时将其年龄增加 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();
        }





    }
}

}

【问题讨论】:

  • 对不起,我在上面说它给了我零,而我的意思是说它给了我原始数字
  • 您返回的是增加的年龄,但实际上并没有改变this.Age
  • 我该如何解决这个问题?
  • this.Age += 1?
  • 不幸的是,我的老师只是希望我在该方法中分配值而不做任何其他事情。这就是为什么我应该创建一个单独的构造函数,将年龄增加 1

标签: java class methods constructor


【解决方案1】:

我为您整理了一些代码。

首先回答您的问题:您返回的是宠物的年龄加 1,实际上并未为宠物的年龄分配新值。为此,请使用Age++Age = Age + 1Age += 1。这应该为宠物的年龄分配一个新值,从而解决您的问题。

以下是我所做的更改:

  • 将字符串声明压缩为一行
  • 固定SetName(String namesetup)。您有声明 namesetup=Name,它将类的 Name 字段分配给方法的实例变量 namesetup。您想将传递给方法的内容分配给由Name=namesetup 完成的类
  • ageincrease() 中的return 语句更改为return Age++; 而不是return Age + 1;。这与Age = Age + 1; 相同
  • switch 语句和while 循环替换了主类中的长while 循环。这很方便,因为您不需要在 while 循环中进行长时间的条件检查。我使用了一个标志变量,它在用户输入"E" 时进行标记。如果您不熟悉switch 语句,那么您可以阅读它们here

注意事项:

  • 使用您当前的代码,您在 while 循环中检查条件 Userinput.equals("D"),但您在 while 循环中没有任何东西可以处理该条件,从而导致无限循环的可能性。
  • 我不会更改您的变量或方法的名称,但您应该知道约定是 variableName,其中第一个单词是小写的,后面所有的第一个字母都是大写的。您的变量和方法名称与代码中的此约定不一致,这可能会使其他编码人员难以阅读和使用。遵循此约定是一种很好的编码实践,可以让您在大型项目中免去麻烦。

这是您编辑的宠物类:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多