-3

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

然后我发现我可以使用get 方法(获得更多分数)。所以我把前面的变量改成了私有的,使用了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);

}

}

4

2 回答 2

0

听起来您正在尝试编写一个执行某些计算的Java程序,而您的计算结果是NaN-您可以参考问题在Java中,NaN是什么意思?有关NaN.

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

于 2012-12-19T01:06:34.953 回答
0

NaN 的原因是这个声明:

    weightDivideHeight = userWeightBmi/userHeightSqaured;

零除以零。换句话说userWeightBmiuserHeightSqaured那时两者都为零。

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

于 2012-12-19T01:51:38.390 回答