为什么当我输入一个整数时,我的 Java 双精度变量类型变成 0.0?
Why is my Java double variable type coming out to 0.0 when I put in an integer?
我正在使用驱动程序 class 创建另一个 class 的对象。当我输入宠物重量或整数时,数字显示为 0.0。所有权重变量都声明为双精度变量,所以我不知道为什么要这样做。
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.");
}
}
这是我的宠物Class
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;
}
}
这个问题的第一个重要的事情是你只需要 class "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" 的容器或实例,不应出现此问题。
如果 age
或 weight
没有预期的格式,我建议检查 getAge()
和 getWeight
的 return 类型。如果它与字段不匹配,它将在 returned 时被转换,至少在 int->double
的情况下(我相信 double->int
需要转换)。
但如前所述,添加 .0
是 double
的预期行为。如果你不想要它,你可以显式地转换为 int
.
我看到的问题是您正在创建多个 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
上只使用那个对象
问题出在第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”的宽度,但扫描仪正在设置 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.
当然其他属性都是空的
希望有用。问候!
错误的是你用这个代码设置值
petName.setWeight(lbs);
并使用它来检索值
Pet petWeight = new Pet();
它们是2 个不同的对象您必须在设置、检索中对 2 个语句使用相同的对象
通过输入
petWeight.setWeight(lbs);
而不是
petName.setWeight(lbs);
会解决的
您在设置和获取您宠物的重量方面做错了。您需要使用同一个对象来设置和获取值。您正在 petName
对象中设置宠物重量并从 petWeight
对象获取。这就是为什么您会得到 0.0
,这是 double
的默认值。解决你的问题
使用相同的对象来设置和获取宠物的体重。
示例:
petWeight.setWeight(lbs); //setting the value in petWight object
petWeight.getWeight(); // it will returns the same value that you set.
感谢大家的帮助。犯了这么简单的错误,我觉得自己像个白痴。我还用 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.");
}
}
我正在使用驱动程序 class 创建另一个 class 的对象。当我输入宠物重量或整数时,数字显示为 0.0。所有权重变量都声明为双精度变量,所以我不知道为什么要这样做。
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.");
}
}
这是我的宠物Class
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;
}
}
这个问题的第一个重要的事情是你只需要 class "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" 的容器或实例,不应出现此问题。
如果 age
或 weight
没有预期的格式,我建议检查 getAge()
和 getWeight
的 return 类型。如果它与字段不匹配,它将在 returned 时被转换,至少在 int->double
的情况下(我相信 double->int
需要转换)。
但如前所述,添加 .0
是 double
的预期行为。如果你不想要它,你可以显式地转换为 int
.
我看到的问题是您正在创建多个 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
上只使用那个对象问题出在第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”的宽度,但扫描仪正在设置 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.
当然其他属性都是空的
希望有用。问候!
错误的是你用这个代码设置值
petName.setWeight(lbs);
并使用它来检索值
Pet petWeight = new Pet();
它们是2 个不同的对象您必须在设置、检索中对 2 个语句使用相同的对象 通过输入
petWeight.setWeight(lbs);
而不是
petName.setWeight(lbs);
会解决的
您在设置和获取您宠物的重量方面做错了。您需要使用同一个对象来设置和获取值。您正在 petName
对象中设置宠物重量并从 petWeight
对象获取。这就是为什么您会得到 0.0
,这是 double
的默认值。解决你的问题
使用相同的对象来设置和获取宠物的体重。
示例:
petWeight.setWeight(lbs); //setting the value in petWight object
petWeight.getWeight(); // it will returns the same value that you set.
感谢大家的帮助。犯了这么简单的错误,我觉得自己像个白痴。我还用 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.");
}
}