【问题标题】:Getting error while creating bmi calculator创建 bmi 计算器时出错
【发布时间】:2019-09-21 13:55:46
【问题描述】:

我正在创建一个 bmi 计算器并使用身高和体重公式。我不断收到一个错误,最后说 nan obese,我不知道我缺少什么或需要编辑什么。任何帮助,将不胜感激。

输出错误:

Enter Weight in pounds: 150
Enter height (ft.): 
6
Enter height (in.): 
7
Your BMI is: NaN
Obese 

BMI公式: BMI = (703 * weightInPounds) / heightInInches^2

代码:

import java.util.Scanner;

public class Bmi_Calc {

    public static void main(String[] args) {
        // TODO Auto-generated method st
        Scanner input = new Scanner(System.in);

        //double weight;
        double weightInPounds;
        int feet; 
        int inches; 
        //int weightInPounds;
        int heightInInches;
        double height;


        System.out.print("Enter Weight in pounds: ");
        double weight = input.nextDouble();

        System.out.println("Enter height (ft.): ");
        feet = input.nextInt();

        System.out.println("Enter height (in.): ");
        inches = input.nextInt();

        weightInPounds = 0;
        heightInInches = 0;

        double bmi = (703 * weightInPounds) / Math.pow(heightInInches, 2.0);
        double heightMeters = ((feet * 12) + inches) * .0254;

        System.out.println("Your BMI is: " + bmi);

        if (bmi < 18.5) {
            System.out.println("Underweight.");
        }

        if ((bmi >= 18.5) && (bmi < 24.9)) {
            System.out.println("Normal weight");
        }

        if ((bmi >= 25) && (bmi < 29.9)) {
            System.out.println("Overwight");
        }

        else {
            System.out.println("Obese");
        }

        input.close();          
    }
}

【问题讨论】:

    标签: java arrays if-statement double


    【解决方案1】:

    检查您的 bmi 计算数字。这会告诉你为什么你会得到 NAN。

    【讨论】:

    • 这不是一个直接的答案,所以作为评论可能更好。
    • 如果你直接给他们答案,一开始他们永远学不会。
    • 确实如此,我的意思是您的答案很可能作为评论更好。
    • 感觉跟双倍高度计有关系?我一直在尝试改变它,但我所做的一切都给出了 heightInInches 以上的错误
    • @dragon40226 感谢您的帮助,我的 doubleheightmeters 没有意义,而且我的身高和体重初始化不正确。
    【解决方案2】:
    weightInPounds = 0;
    heightInInches = 0;
    
    double bmi = (703 * weightInPounds) / Math.pow(heightInInches, 2.0); 
    

    在上面weightInPoundsheightInInches 为零,所以bmi = 0/0 导致NaN

    谢谢

    【讨论】:

    • 我无法删除它们,因为它们需要被初始化,并且通过添加 1 而不是零,它会将下面几行中的值更改为 1,从而使您的 bmi 703 自动
    • @Homsey 在进行除法之前,它们需要使用正确的值、实际身高和体重进行初始化,而不是任意数字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多