Java 使用get方法时获取NaN
我正在做一个体重指数计算器,为我的一个课程做一个节食计划。最初,我将两个变量作为公共静态变量,以从另一个类获取变量。我的体重指数计算器就是这样工作的 然后我发现我可以使用get方法获得更多分数。因此,我将前面的变量改为private,并使用get方法。但是当我运行这个程序时,当程序打印出保存BMI的变量时,我得到了NaN,这以前从未发生过 有人能帮忙吗Java 使用get方法时获取NaN,java,methods,get,nan,Java,Methods,Get,Nan,我正在做一个体重指数计算器,为我的一个课程做一个节食计划。最初,我将两个变量作为公共静态变量,以从另一个类获取变量。我的体重指数计算器就是这样工作的 然后我发现我可以使用get方法获得更多分数。因此,我将前面的变量改为private,并使用get方法。但是当我运行这个程序时,当程序打印出保存BMI的变量时,我得到了NaN,这以前从未发生过 有人能帮忙吗 import java.util.Scanner; public class Weight { private Scanner input;
import java.util.Scanner;
public class Weight {
private Scanner input;
private String readInput;
private String userWeightIsPounds;
private String userWeightIsStones;
private Scanner input2;
public static double userWeight;
public Weight(){
userWeightIsPounds = ("Pounds");
userWeightIsStones = ("Stones");
}
public void findOutUserWeightMessage(){
System.out.println("Firstly Do you weigh yourself in pounds or stones?");
}
public void findOutUserWeight(){
input = new Scanner (System.in);
readInput = input.nextLine();
if(readInput.equals(userWeightIsPounds)){
System.out.println("Ok then, enter your weight in pounds please.");
}
if(readInput.equals(userWeightIsStones)){
System.out.println("Ok enter your weight in stones please.");
}
input2 = new Scanner (System.in);
userWeight = input2.nextFloat();
if (userWeight > 20){
System.out.println("You've enetered your weight as " + userWeight + " lbs. I'll save that information for later.");
}else{
userWeight = userWeight * 14;
System.out.println("I've converted your weight into pounds for you. You weigh " + userWeight + " lbs. I'll save that information for later.");
}
}
public double static getUserWeight(){
return userWeight;
}
}
还有一个计算类的代码。忽略一些println,我试图找出我的变量发生了什么
public class BMI {
private double userHeightSqaured;
private double bmiMutiplier;
private double weightDivideHeight;
private double userBmi;
private double userWeightBmi;
private double userHeightBmi;
BMI(){
bmiMutiplier = 703;
userWeightBmi = Weight.getUserWeight();
userHeightBmi = Height.getUserHeight();
}
public void startUpBmiMessage(){
System.out.print("Lets start with your BMI then shall we? ");
}
public void calculateUserBmi(){
System.out.println("userWeightBmi is " + userWeightBmi);
System.out.println("userWeightBmi is " + userHeightSqaured);
userHeightSqaured = userHeightBmi * userHeightBmi;
System.out.println("userHeightSqaured is " + userHeightSqaured);
weightDivideHeight = userWeightBmi/userHeightSqaured;
System.out.println("weightDivideHeight is " + weightDivideHeight);
userBmi = weightDivideHeight * bmiMutiplier;
System.out.println("weightDivideHeight is " + weightDivideHeight);
System.out.println("bmiMutiplier is " + bmiMutiplier);
}
public void calculateUserBmiMessage(){
System.out.println("Your bmi is " + userBmi);
}
}听起来好像你在试图编写一个Java程序来执行一些计算,而计算的结果是NaN——你可以参考这个问题来了解有关NaN的一些信息
至于在没有看到任何代码的情况下解决您的问题,并且假设您的计算在以前使用相同的输入时运行良好,听起来您使用getter从公共静态变量切换到私有变量可能会使一些变量未初始化,因此,它们的值默认为0-除以0是NaN的常见原因。NaN的原因是以下语句:
weightDivideHeight = userWeightBmi/userHeightSqaured;
零除以零。换句话说,userWeightBmi和userHeightSqaured在那个点上都是零
根本的问题似乎是你没有意识到静态变量和实例变量之间的区别。当您应该/不应该使用这两种变量时。请只看一个提示-关键字static不应该出现在代码中的任何地方,除了courseThanks removed static的主方法。但是eclipse一直告诉我要将修饰符更改为static@乔纳桑坎宁-你明白静态是什么意思吗???什么是静态变量?静态方法是什么?为什么不能从静态方法访问实例变量?回到你的课堂讲稿/课本/任何东西上,仔细阅读这些主题。