Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5个月前关闭。
我的程序必须创建两个狗对象并为其赋予属性。在Dog类中,我必须有一个带有跳蚤的构造函数和一个不带跳蚤的构造函数。使用该程序时,我从输出之一中的错误对象接收到信息。
我应该得到“ Dog2的名字是test,它的品种是金毛,它的性别是F,它的月龄是12,它的体重(磅)是9.0”,但是我得到Dog2的名字是Luca,它的品种是Golden Retriever,性别为F,月龄为12,体重为9.0
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5个月前关闭。
我的程序必须创建两个狗对象并为其赋予属性。在Dog类中,我必须有一个带有跳蚤的构造函数和一个不带跳蚤的构造函数。使用该程序时,我从输出之一中的错误对象接收到信息。
public class JavaProgram{
public static void main (String [] args){
Dog dog1 = new Dog ("Luca", "mutt", 'M', 22, 5 );
Dog dog2 = new Dog ("test", "Golden Retriever", 'F', 12, 9, true );
System.out.println("Dog1's name is " + dog1.getName() + ", its breed is " +
dog1.getBreed() + ", its sex is " + dog1.getSex() + ", its age in months is " +
dog1.getAge() + ", its weight in pounds is " + dog1.getWeight());
System.out.println("When Dog1 eats it makes the noise " + dog1.getEating() +
", and when its barks the noise made is " + dog1.getBarking());
System.out.println("Dog2's name is " + dog1.getName() + ", its breed is " +
dog2.getBreed() + ", its sex is " + dog2.getSex() + ", its age in months is " +
dog2.getAge() + ", its weight in pounds is " + dog2.getWeight());
System.out.println("When Dog2 eats it makes the noise " + dog2.getEating() +
", and when its barks the noise made is " + dog2.getBarking() + "It does" + dog2.getFleas());
}
}
public class Dog{
private String name;
private String breed;
private char sex;
//In months
private int age;
//In pounds
private double weight;
private boolean fleas;
private String yesFleas;
private String noFleas;
private String eating;
private String barking;
public Dog(String name, String breed, char sex, int age, double weight){
this("Chomp, chomp, chomp", "Woof, woof, woof");
this.name = name;
this.breed = breed;
this.sex = sex;
this.age = age;
this.weight = weight;
}
public Dog(String name, String breed, char sex, int age, double weight, boolean fleas){
this( "Chomp, chomp, chomp", "Woof, woof, woof", "have fleas, scratch, scratch, scratch", "not have fleas, yay!");
this.name = name;
this.breed = breed;
this.sex = sex;
this.age = age;
this.weight = weight;
this.fleas = fleas;
}
public Dog(String eating, String barking){
this.eating = eating;
this.barking = barking;
}
public Dog(String eating, String barking, String yesFleas, String noFleas){
this.eating = eating;
this.barking = barking;
this.yesFleas = yesFleas;
this.noFleas = noFleas;
}
public String getName(){
return name;
}
public String getBreed(){
return breed;
}
public char getSex(){
return sex;
}
public int getAge(){
return age;
}
public double getWeight(){
return weight;
}
public String getEating(){
return eating;
}
public String getBarking(){
return barking;
}
public String getFleas(){
if (fleas == true)
return yesFleas;
else
return noFleas;
}
}
我应该得到“ Dog2的名字是test,它的品种是金毛,它的性别是F,它的月龄是12,它的体重(磅)是9.0”,但是我得到Dog2的名字是Luca,它的品种是Golden Retriever,性别为F,月龄为12,体重为9.0
最佳答案
那是因为您要打印dog1的名称而不是dog2:
System.out.println("Dog2's name is " + dog1.getName()...
^^^
10-04 20:06