Java中的BMI计算器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18475971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:57:46  来源:igfitidea点击:

BMI calculator in Java

java

提问by ChrisD93

I am new to Java and still trying to learn my way around it. Today my teacher gave an assignment where we make a BMI calculator in Java. One of the steps is to make it show the BMI categories. So the user would be able to look at it and see where they stand. I got everything else done but am running into an issue.

我是 Java 新手,仍在努力学习解决它。今天我的老师布置了一个作业,我们用 Java 制作一个 BMI 计算器。步骤之一是使其显示 BMI 类别。因此,用户将能够查看它并了解他们所处的位置。我完成了其他所有工作,但遇到了问题。

Here is the script:

这是脚本:

import java.util.Scanner;

public class BMI {
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        double weight = 0.0;
        double height = 0.0;
        double bmi = 0.0;

        System.out.print("Enter your weight in pounds: ");
        weight = input.nextInt();

        System.out.print("Enter your height: ");
        height = input.nextInt();

        bmi = ((weight * 703)/(height * height));

        System.out.printf("Your BMI is", bmi);

        System.out.println("BMI VALUES");
        System.out.println("Underweight: Under 18.5");
        System.out.println("Normal: 18.5-24.9 ");
        System.out.println("Overweight: 25-29.9");
        System.out.println("Obese: 30 or over");
     }
    }

Here is the result:

结果如下:

Your BMI isBMI VALUES
Underweight: Under 18.5
Normal: 18.5-24.9 
Overweight: 25-29.9
Obese: 30 or over

What am I doing wrong and how do I fix this?

我做错了什么,我该如何解决?

回答by ChrisD93

Try this:

尝试这个:

System.out.printf("Your BMI is %f\n", bmi);

You can also print like this:

你也可以这样打印:

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

回答by rgettman

You forgot the placeholder for the bmi(and the line terminator):

您忘记了bmi(和行终止符)的占位符:

System.out.printf("Your BMI is %f\n", bmi);

Here's the formatting syntax guidefor your reference.

这是供您参考的格式语法指南

回答by JoshDM

While you are using printf, the following also works:

在您使用时printf,以下内容也有效:

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

回答by Prashant Bhate

Ideally you should print floats / doubles in a readable format Normally BMI value is measured up to 2 decimal place

理想情况下,您应该以可读格式打印浮点数/双精度数 通常 BMI 值测量到小数点后 2 位

System.out.format("Your BMI is %.2f%n",bmi);

回答by Dan Osborne

It's probably more coding but you could use JOptionPane

这可能是更多的编码,但你可以使用 JOptionPane

input = JOptionPane.showInputDialog("What is you weight in pounds?");
        weight = Double.parseDouble(input);

        input = JOptionPane.showInputDialog("What is your hight is inches?");
        height = Double.parseDouble(input);

回答by ayham.oracle

mismatch between variable type declaration (double) and your declaration of user input type (nextInt) I would use nextDouble as an input type if you want to maintain variable type as a double.

变量类型声明 (double) 和您的用户输入类型声明 (nextInt) 之间不匹配 如果您想将变量类型保持为 double,我将使用 nextDouble 作为输入类型。

回答by Mofreh Sarhan

A: I am not sure that bmi = ((weight * 703)/(height * height));is right.

答:我不确定这bmi = ((weight * 703)/(height * height));是对的。

I came across a formula which might work for you BMI = ((weight/(height * height))* 10000;

我遇到了一个可能对你有用的公式 BMI = ((weight/(height * height))* 10000;

B: System.out.printf(“Your BMI is”, bmi);is not needed System.out.prinln( “ Your BMI is “ + BMI);

B: System.out.printf(“Your BMI is”, bmi);不需要 System.out.prinln( “ Your BMI is “ + BMI);

I hope it work for you best of luck keep it up

我希望它对你有用,祝你好运保持下去

回答by Rangna

You can use arrays like this:

您可以像这样使用数组:

 public static void main(String[] args) {
    double[] ht = new double[5];
    double[] wt = new double[5];
    double[] bmi = new double[5];
    String[] sht = new String[5];
    String[] swt = new String[5];

    for (int i = 0; i < ht.length; i++) {
        sht[i] = JOptionPane.showInputDialog("Enter Hight");
        swt[i] = JOptionPane.showInputDialog("Enter Weight");
        ht[i]=Double.parseDouble(sht[i]);
        wt[i]=Double.parseDouble(swt[i]);
        bmi[i]=wt[i]/(ht[i]*ht[i]);
        System.out.println("BMI IS "+bmi[i]+"Kg/m2");
        JOptionPane.showMessageDialog(null,"BMI IS  "+bmi[i] +"Kg/m2");
        if(bmi[i]>25){
            JOptionPane.showMessageDialog(null,"YOU ARE AT RISK");
            System.out.println("YOU ARE AT RISK");
        }else {
           JOptionPane.showMessageDialog(null,"YOU ARE Healthy"); 
           System.out.println("YOU ARE Healthy ");
    }

    }
}

回答by Argha Das

https://github.com/arghadasofficial/BMI/blob/master/src/argha/bmi/Bmi.java

https://github.com/arghadasofficial/BMI/blob/master/src/argha/bmi/Bmi.java

public class Bmi {

    //Variables
    private double result; //Stores the result
    private String bmi; //Stores the Bmi result in String
    private String status; //Stores the status (Normal......)
    private static DecimalFormat decimalFormat = new DecimalFormat(".#"); //Formating the result

    /**
     * Get the BMI from calculation and returns the result in String format
     *
     * @return BMI
     */
    public String getBmi() {
        /**
         * Following code example : bmi = decimalFormat.format(result);
         *
         * if bmi is 23.12345 then remove 2345 and return 23.1 using DecimalFormat class provided by Java
         *
         */
        bmi = decimalFormat.format(result);
        return bmi;
    }

    /**
     * Get the status according to the BMI result and returns the Status in
     * String format
     *
     * @return Status
     */
    public String getStatus() {
        return status;
    }

    /**
     * Get weight and height in double format and do the calculation for you
     *
     * @param weight - accepts weight in kilograms
     * @param height - accept height in centimeters
     */
    public void calculateBmi(double weight, double height) {
        /**
         * First get weight and height then convert height to meters then
         * multiply the height itself to height and then divide the height with
         * weight then set the actual value to result variable
         */

        //Convert the height from cm to m
        double meters = height / 100;

        //Multiply height by height
        double multiply = meters * meters;

        //divide the height with weight in kg
        double division = weight / multiply;

        //set the value to result variable
        result = division;

        //call checkStatus method for checking Status
        checkStatus();
    }

    /**
     * Private method for checking bmi and returns the status It remains private
     * because we don't need to access this from somewhere else.
     */
    private void checkStatus() {
        /**
         * Check :
         *
         * if BMI is less than 18.5 then set status to Underweight if BMI is
         * greater than 18.5 and less than 25 then set status to Normal if BMI
         * is greater than 25 and less than 30 then set status to Overweight if
         * BMI equals to 30 or greater than 30 then set status to Obese
         */
        if (result < 18.5) {
            status = "Underweight";
        } else if (result > 18.5 && result < 25) {
            status = "Normal";
        } else if (result > 25 && result < 30) {
            status = "Overweight";
        } else if (result == 30 || result > 30) {
            status = "Obese";
        }
    }
}