【问题标题】:Getting NaN when using get method使用 get 方法时获取 NaN
【发布时间】:2012-12-06 07:22:23
【问题描述】:

我正在为我正在为课程作业制作的饮食编程制作 BMI 计算器。最初我制作了几个变量 public static 来从另一个类中获取变量。我的 BMI 计算器以这种方式运行良好。

然后我发现我可以使用 get 方法(获得更多分数)。所以我将前面的变量改为private,并使用了get 方法。但是当我运行这个程序时,当程序打印出保存 BMI 的变量时,我得到 NaN,这以前从未发生过!

谁能帮忙?

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);

}

}

【问题讨论】:

  • 只是一个提示 - 关键字 static 不应出现在代码中的任何位置(当然,main 方法除外)
  • 感谢删除静态。但是eclipse一直告诉我要把修饰符改成static!
  • @JonathanCanning - 你明白static是什么意思吗? static 变量是什么? static 方法是什么?为什么不能从静态方法访问实例变量?回到你的讲义/教科书/任何东西,阅读这些主题。

标签: java methods get nan


【解决方案1】:

听起来您正在尝试编写一个执行某些计算的 Java 程序,而您的计算结果是 NaN - 您可以参考问题 In Java, what does NaN mean? 以获取有关 NaN 的一些信息。

至于在没有看到任何代码的情况下解决您的问题,并假设您的计算在之前使用相同的输入时运行良好,听起来您从 public static 变量切换到带有 getter 的私有变量可能会使您的一些变量未初始化,所以它们的值默认为 0 - 除以 0 是NaN 的常见原因。

【讨论】:

    【解决方案2】:

    NaN 的原因是这个语句:

        weightDivideHeight = userWeightBmi/userHeightSqaured;
    

    用零除以零。换句话说,userWeightBmiuserHeightSqaured 在那时都为零。

    根本问题似乎是您没有弄清楚static 和实例变量之间的区别。以及何时应该/不应该使用这两种变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多