Java - 代码不会改变狗的人类年龄

Java - code won't transform dog age in human years


我正在尝试编写一个程序,将狗的输入年龄转换为人的年龄,但它不起作用。以下是我将狗年转换为人类年的说明:

一种 inHumanYears 方法,它将 return 宠物狗的年龄以人类年计算。计算方法如下:

举几个例子:

到目前为止,我的代码是这样的:

import java.util.Scanner;
public class MyPet_1_lab7 {
    // Implement the class MyPet_1 so that it contains 3 instance variables
        private String breed;
        private String name;
        private int age;
        private double inHumanYears;

    // Default constructor
        public MyPet_1_lab7()
        {
            this.breed = null;
            this.name = null;
            this.age = 0;
        }
    // Constructor with 3 parameters
        public MyPet_1_lab7(String a_breed, String a_name, int an_age){
            this.breed = a_breed;
            this.name = a_name;
            this.age = an_age;
            this.inHumanYears = inHumanYears();
        }
    // Accessor methods for each instance variable
        public String getBreed(){
            return this.breed;
        }
        public String getName(){
            return this.name;
        }
        public int getAge(){
            return this.age;
        }
    //Mutator methods for each instance variable
        public void setBreed(String a_breed){
            this.breed = a_breed;
        }
        public void setName(String a_name){
            this.name = a_name;
        }
        public void setAge(int an_age){
            this.age = an_age;
            this.inHumanYears = inHumanYears();
        }
    // toString method that will return the data in an object formated as per the output
        public String toString(){
            return (this.breed + " whose name is " + this.name + " and " + (double)this.age + " dog years (" + inHumanYears() + " human years old)");
        }
        public boolean equals(MyPet_1_lab7 a){
            if ((this.breed.equals(a.getBreed())) && (this.age == a.getAge())){
                return true;
            }
            else
                return false;
        }
        public double inHumanYears(){
            if ((double)age >= 2 ){
                inHumanYears = (15 + 9 + (age - 2))*5;
                return (inHumanYears);
                }
            else {
                inHumanYears = age*15;
                return (inHumanYears);
            }
        }
        public double getHumanYears(){
            double yearOneAge = age >=1 ? 1.0: age;
            double yearTwoAge = age >=2 ? 1.0: age > 1? age-1.0: 0.0;
            double yearThreeAge = age > 2 ? age-2.0: 0.0;

            inHumanYears = yearOneAge * 15 + yearTwoAge*9 + yearThreeAge * 5;
            return inHumanYears;
        }
    public static void main(String[] args) {
        Scanner keyboard = new Scanner (System.in);
        System.out.print("What type of dog do you have? ");
        String breed = keyboard.nextLine();

        System.out.print("What is its name? ");
        String name = keyboard.nextLine();

        System.out.print("How old? ");
        int age = keyboard.nextInt();
        MyPet_1_lab7 dog= new MyPet_1_lab7();
        System.out.println(dog);
        MyPet_1_lab7 dog1 = new MyPet_1_lab7(breed,name,age);
        System.out.println(dog1);



    }

}
'''

问题是您的 toString() 方法访问了您使用尚未调用的方法 设置 的字段。这个

public String toString(){
    return (this.breed + " whose name is " + this.name + " and " 
            + this.age + " dog years (" + inHumanYears + " human years old)");
}

应更改为调用 inHumanYears()。喜欢,

public String toString(){
    return (this.breed + " whose name is " + this.name + " and " 
            + this.age + " dog years (" + inHumanYears() + " human years old)");
}

您的方法 InHumanYears() 从未被调用 + 您的属性 inHumanYears 从未被赋值。

您必须更改第二个构造函数。

之前:

 public MyPet_1_lab7(String a_breed, String a_name, int an_age){
        this.breed = a_breed;
        this.name = a_name;
        this.age = an_age;
    }

之后:

public MyPet_1_lab7(String a_breed, String a_name, int an_age){
        this.breed = a_breed;
        this.name = a_name;
        this.age = an_age;
        this.inHumanYears = inHumanYears();
    }

您还必须在 setAge() 中调用 inHumanYears() :

setAge(int age){
  this.age = age;
  this.inHumanYears = inHumanYears();
}

我得到这个执行(我认为你的计算不正确):

toto whose name is tata and 3 dog years (125.0 human years old)

您的算法似乎不符合规定的要求。

  1. 人类的 15 年相当于中型犬的第一年。
  2. 狗的第二年大约等于人类的九年。
  3. 在那之后,每个人类年大约是五年 为了一只狗。

您还应该更改年龄以允许小数点。 (如果不是年龄可能以月为单位,就像您 4 个月大的狗的示例 -> 在这种情况下,您应该将所有内容除以 12)。通常最好将其命名为 ageMonth 或 ageYear 以消除歧义。

您可以尝试分解每个组件。

注意:?: 是 ternary operators。将其视为 if-else 语句

的 "short form"
public double getHumanYears(){


    double yearOneAge = age>=1 ? 1.0: age;
//if age>1, then year1Age=1, otherwise year1Age = age

    double yearTwoAge = age>=2 ? 1.0: age>1? age-1.0: 0.0;
//if age>2, then year2Age=2, elseIf age>1, then year2Age = age-1, otherwise, year2Age is 0.

    double yearThreeAge = age>2 ? age-2.0: 0.0;
//if age>2, then year3Age= age-2, otherwise, year3Age is 0.

//the formula will break down an age into 3 parts.
//yearOneAge: from 0.0 to 1.0. 
//yearTwoAge: from 0.0 to 1.0. 
//yearThreeAge: from 0.0 onwards. 
//e.g. age=0.8years, year1=0.8, year2=0.0, year3=0.0
//e.g. age=1.5years, year1=1.0, year2=0.5, year3=0.0
//e.g. age=3.6years, year1=1.0, year2=1.0, year3=1.6

    inHumanYears = yearOneAge * 15 + yearTwoAge * 9 + yearThreeAge * 5
    return inHumanYears; 

}

也像 Quentin 所说,您忘记在 setter 和构造函数中调用 getHumanYears,或者您可以更新 toString 以调用 getHumanYears()。

好的,谢谢大家的宝贵时间和帮助。所以这就是我所做的,我将 private int age 更改为 double。我的朋友告诉我,我们不一定要把它变成一个整数。剩下的只是将所有内容都更改为两倍,它解决了我输出的所有问题:

import java.util.Scanner;
public class MyPet_1_lab7 {
    // Implement the class MyPet_1 so that it contains 3 instance variables
        private String breed;
        private String name;
        private double age; // instead of private int age;

        MyPet_1_lab7 dog1;
        MyPet_1_lab7 dog2;

    // Default constructor
        public MyPet_1_lab7()
        {
            this.breed = null;
            this.name = null;
            this.age = 0;
        }
    // Constructor with 3 parameters
        public MyPet_1_lab7(String a_breed, String a_name, double an_age){
            this.breed = a_breed;
            this.name = a_name;
            this.age = an_age;
        }
    // Accessor methods for each instance variable
        public String getBreed(){
            return this.breed;
        }
        public String getName(){
            return this.name;
        }
        public double getAge(){
            return this.age;
        }
    //Mutator methods for each instance variable
        public void setBreed(String breed2){
            this.breed = breed2;
        }
        public void setName(String name2){
            this.name = name2;
        }
        public void setAge(double age2){
            this.age = age2;
        }
    // toString method that will return the data in an object formated as per the output
        public String toString(){
            return (this.breed + " whose name is " + this.name + " and " + this.age + " dog years (" + inHumanYears() + " human years old)");
        }
        public boolean equals(MyPet_1_lab7 a){
            if ((this.breed.equals(a.getBreed())) && (this.age == a.getAge())){
                return true;
            }
            else
                return false;
        }
        double human_age = 0;
        public double inHumanYears(){
            if (this.age < 1)
                human_age = (this.age) * 15;
            if (this.age >=1 && this.age < 2)
                human_age = 15 + (this.age - 1) * 9;
            if (this.age >= 2 ){
                human_age = 15 + 9 + (age - 2)*5;
                }

            return human_age;
        }

    public static void main(String[] args) {
        Scanner keyboard = new Scanner (System.in);
        System.out.print("What type of dog do you have? ");
        String breed = keyboard.nextLine();

        System.out.print("What is its name? ");
        String name = keyboard.nextLine();

        System.out.print("How old? ");
        double age = keyboard.nextDouble();
        MyPet_1_lab7 dog= new MyPet_1_lab7();
        System.out.println(dog);
        MyPet_1_lab7 dog1 = new MyPet_1_lab7(breed,name,age);
        System.out.println(dog1);

        Scanner key = new Scanner(System.in);
        System.out.println("\nLet's set up the 1st dog ... ");
        System.out.print("\tWhat breed is it? ");
        String breed1 = key.nextLine();

        System.out.print("\tWhat is the dog's name? ");
        String name1 = key.nextLine();

        System.out.print("\tHow old is the dog in dog years (a double number)? ");
        double age1 = key.nextDouble();
        MyPet_1_lab7 dog2 = new MyPet_1_lab7 (breed1, name1, age1);
        System.out.println("Dog1 is now a(n) " + dog2);

        System.out.println("\nAre the 2 dogs the same breed and age?");
        if ((breed.equals(breed1))&& age == age1)
            System.out.println("Yes, they are the same breed and age");
        else 
            System.out.println("No, they are not the same breed and/or age");
    }
}
'''