首页 > 解决方案 > Java - 代码不会改变人类年龄的狗年龄

问题描述


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

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

这里举几个例子:

所以这是我的代码到目前为止的样子:

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



    }

}
'''

标签: javacalculatorconverters

解决方案


问题是您的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)");
}

推荐阅读