我正在使用驱动程序类来创建另一个类的对象。当我输入宠物重量或整数时,数字变为0.0。所有的权重变量都被声明为double,所以我不知道为什么会这样做。
import java.util.Scanner;
public class PetAssignment {
public static void main(String[] args)
{
String nameAndType;
int yrs;
double lbs;
//Scanner object for the keyboard input
Scanner answers = new Scanner(System.in);
//Pet objects used for calling accessor methods
Pet petName = new Pet();
Pet petType = new Pet();
Pet petAge = new Pet();
Pet petWeight = new Pet();
//A bunch of other code and pet attributes
//Input for the weight of pet
System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
petName.setWeight(lbs);
//Print out of the user's answers
System.out.println("");
System.out.println("You have a "+ petType.getType() + ". That is named "
+ petName.getName()+ " and is "
+ petAge.getAge() + " years old and weighs "
+ petWeight.getWeight() + " lbs.");
}
}
这是我的宠物类
public class Pet
{
private String name;
private String type;
private int age;
private double weight;
/*
* a bunch of other code
*/
public void setWeight(double petWeight)
{
weight = petWeight;
}
/*
* a bunch of other code
*/
public double getWeight()
{
return weight;
}
}
答案 0 :(得分:2)
这个问题的第一个重要问题是你只需要一个“Pet”类的实例。一只宠物可以容纳你需要的所有变量。 例如:
Pet pet = new Pet();
System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
petName.setWeight(lbs);
System.out.println("");
System.out.println("You have a "+ pet.getType() + ". That is named "
+ pet.getName()+ " and is "
+ pet.getAge() + " years old and weighs "
+ pet.getWeight() + " lbs.");
你写它的方式基本上只需要一个就可以创建4个不同的容器。您已将权重分配给petWeight容器,但随后尝试从petName容器中获取权重,这导致您检索错误的变量。在这种情况下,只有一个容器或实例称为“pet”,不应发生此问题。
答案 1 :(得分:2)
错误的是您使用此代码设置值
petName.setWeight(lbs);
并使用它来检索值
Pet petWeight = new Pet();
它们是 2个不同的对象您必须在设置和检索中对2个语句使用相同的对象 通过推荐
petWeight.setWeight(lbs);
而不是
petName.setWeight(lbs);
将解决它
答案 2 :(得分:1)
如果age
或weight
没有预期的格式,我建议您检查getAge()
和getWeight
的返回类型。如果它与字段不匹配,则会在返回时进行转换,至少在int->double
情况下(我相信double->int
需要转换)。
但如前所述,添加.0
是double
的预期行为。如果您不想要,可以明确转换为int
。
答案 3 :(得分:1)
我看到的问题是你正在创建多个Pet对象并将宠物重量分配给" petName"对象然后你试图在输出中添加" petWeight"的重量。宾语。 请尝试以下操作:
//Input for the weight of pet
System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
petWeight.setWeight(lbs);
//Print out of the user's answers
System.out.println("");
System.out.println("You have a "+ petType.getType() + ". That is named "
+ petName.getName()+ " and is "
+ petAge.getAge() + " years old and weighs "
+ petWeight.getWeight() + " lbs.");
}
}
另外,我建议只使用一个物品" pet"并将每个值分配给该值,并在system.out
上仅使用该值答案 4 :(得分:1)
问题出在第34行,检查你在这做什么
petName.setWeight(lbs);
但是当你显示输出
时System.out.println("You have a "+ petType.getType() + ". That is named "
+ petName.getName()+ " and is "
+ petAge.getAge() + " years old and weighs "
+ petWeight.getWeight() + " lbs.");
你看到了吗?你正在显示" petWeight的wieight,但扫描仪正在设置petName对象,请调试并检查它。
System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
petWeight.setWeight(lbs);
结果是
You have a null. That is named null and is 0 years old and weighs 1.5 lbs.
其他属性为空。
我希望这很有用。此致!
答案 5 :(得分:1)
感谢大家的帮助。有这么简单的错误,我觉得自己像个白痴。我还用一个pet class实例简化了我的代码。
import java.util.Scanner;
public class PetAssignment {
public static void main(String[] args)
{
String nameAndType;
int yrs;
double lbs;
//Scanner object for the keyboard input
Scanner answers = new Scanner(System.in);
//Pet objects used for calling accessor methods
Pet pet = new Pet();
//A bunch of other code and pet attributes
//Input for the weight of pet
System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
pet.setWeight(lbs);
//Print out of the user's answers
System.out.println("");
System.out.println("You have a "+ pet.getType() + ". That is named "
+ pet.getName()+ " and is "
+ pet.getAge() + " years old and weighs "
+ pet.getWeight() + " lbs.");
}
}
答案 6 :(得分:0)
你在设置和获得你的宠物重量方面做错了。您需要使用相同的对象来设置和获取值。您正在petWeight
对象中设置宠物重量并从0.0
对象获取。这就是为什么您获得double
这是petWeight.setWeight(lbs); //setting the value in petWight object
petWeight.getWeight(); // it will returns the same value that you set.
的默认值的原因。解决你的问题
使用相同的物体来设定并获得宠物的体重。
示例:
{{1}}