java - Java - 代码不会改变人类年龄的狗年龄
问题描述
我正在尝试做一个程序,将狗的输入年龄转换为人类年龄,但它不起作用。以下是我将狗年转换为人类年的说明:
一种inHumanYears方法,它将以人类年返回宠物狗的年龄。以下是计算方法:
- 人类的 15 年等于中型犬生命的第一年。
- 狗的第二年相当于人类的九年。
- 在那之后,人类的每一年对于一只狗来说大约是五年。
这里举几个例子:
- 一只 4 个月大的狗 = 0.25(即一年的 1/4)*15 = 3.75 人年
- 5 岁 = 15+9+(5-2) * 5 = 39 人类年
所以这是我的代码到目前为止的样子:
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)");
}
推荐阅读
- r - 使用某些 plyr 函数来计算不止一件事
- javascript - 无法使用 chart.js 2.7 生成多轴组合图(条形图/线形图)
- r - 按因子 B 的数值对因子 A 重新排序
- java - 从 java servlet 下载时文件损坏
- twitter-bootstrap - Bootstrap 和 Zeplin.IO
- python - 如何通过索引每个级别的 agg='sum' 的总数对多索引 pandas 数据帧数据透视表进行排序
- python - Wagtail:将 Orderable 类添加到 Orderable 类
- git - 如何忽略 git 存储库子目录中除单个文件之外的所有内容?
- r - ggplot2混淆矩阵geom_text标签
- c# - 寻找更优雅的 DO WHILE 解决方案