我正在尝试做一个程序,将输入的狗的年龄转换成人类的岁月,但这是行不通的。这是我将狗年转换为人年的说明:
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);
}
}
'''
答案 0 :(得分:1)
问题是您的toString()
方法使用尚未调用的方法访问您 set 设置的字段。这个
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)");
}
答案 1 :(得分:0)
您的方法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的名字是塔塔和3个狗年(125.0人岁)
答案 2 :(得分:0)
您的算法似乎对所陈述的要求不正确。
还应该更改年龄以允许小数点。 (如果不是年龄,则可能以月为单位,例如您的4个月大狗的示例->在这种情况下,应将所有内容除以12)。通常最好将它命名为ageMonth或ageYear以消除这种歧义。
您可以尝试分解每个组件。
注意:?:是ternary operators。将其视为if-else语句的“简短形式”
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所说的那样,您忘记了在设置和构造函数中调用getHumanYears,或者可以更新toString来调用getHumanYears()。
答案 3 :(得分:0)
好,谢谢大家的时间和帮助。所以这就是我所做的,我将私有int年龄更改为两倍。我的朋友告诉我,我们不一定需要将其放入整数。剩下的就是将所有内容都翻倍,这解决了我所有的输出问题:
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");
}
}
'''