我刚刚开始学习 Java,我正在尝试使用 OOP 概念创建一个简单的 BMI 计算器。
这是我的代码:
import java.util.Scanner;
class BIM
{
int weight;
double weightKG = weight * 0.45359237;
int height;
double heightMETERS = height * 0.0254;
double BMI = weightKG / (heightMETERS * heightMETERS);
public void calculateBMI()
{
System.out.print("The BMI is: " + BMI);
}
}
public class BMI
{
public static void main(String args[] ) throws Exception
{
Scanner input = new Scanner(System.in);
System.out.print("Enter weight in pounds: ");
int weight = input.nextInt();
System.out.print("Enter height in inches: ");
int height = input.nextInt();
BIM b = new BIM();
b.calculateBMI();
}
private static void bmiCategory (double BMI)
{
if (BMI < 18.5)
{
System.out.println("Underweight");
}
else if (BMI < 25.0)
{
System.out.println("Normal");
}
else if (BMI < 30.0)
{
System.out.println("Overweight");
}
else
{
System.out.println("Obese");
}
}
}
我的代码的输出:
输入体重(磅):100 输入高度(英寸):50 BMI 为:NaN
为什么我得到输出 NaN?另外,为什么不显示类别?
您的
BIM
类会完成所有数学运算,但您从未将 height
或 weight
传递给它(并且在将这些值分配到任何地方之前先进行数学运算)。首先,修复BIM
。比如,
class BIM {
public BIM(int weight, int height) {
this.weight = weight;
this.height = height;
this.weightKG = weight * 0.45359237;
this.heightMETERS = height * 0.0254;
this.BMI = weightKG / (heightMETERS * heightMETERS);
}
int weight;
double weightKG;
int height;
double heightMETERS;
double BMI;
public void calculateBMI() {
System.out.print("The BMI is: " + BMI);
}
}
然后代替
BIM b = new BIM();
你需要
BIM b = new BIM(weight, height);
错误1
您没有为
BMI 类的对象设置
weight
和 height
属性的值。您需要在您的BMI类中添加以下代码:
BIM(int weight, int height){
this.weight = weight;
this.height = height;
}
然后创建一个BMI类的对象作为
BIM b = new BIM(weight, height);//passing parameters to constructor
错误2
您没有在任何地方调用
bmiCategory
函数,因此未显示类别。
问题1:双倍BMI = 体重KG / (身高METERS * 身高METERS);
您正在尝试将weightKG除以零,请参阅下面的内联评论
**
import java.util.Scanner;
class BIM
{
int weight;
double weightKG = weight * 0.45359237;
int height; // Here value of height is 0 as you are not assigning the value .
double heightMETERS = height * 0.0254; // anything multiplied with 0 height is 0 , heightMETERS value will be 0
// The below statement divides weightKG by zero which is infinite and it is not a number
double BMI = weightKG / (heightMETERS * heightMETERS);
public void calculateBMI()
{
System.out.print("The BMI is: " + BMI); // you will sendup printing NaN
}
}
**
问题2:你永远不会调用bmiCategory方法来打印基于BMI的类别。
如果我要重新格式化和重新排列这段代码,我会这样做。我们仍然可以通过异常处理来改进这一点,但至少我们可以做到这一点。
**
import java.util.Scanner;
class Bmi {
public double calculateBMI(double weight, double height) {
double weightKG = weight * 0.45359237;
double heightMETERS = height * 0.0254;
double bmi = weightKG / (heightMETERS * heightMETERS);
return bmi;
}
public String bmiCategory(double bmi) {
if (bmi < 18.5) {
return "Underweight";
}
if (bmi < 25.0) {
return "Normal";
}
if (bmi < 30.0) {
return "Overweight";
}
return "Obese";
}
}
public class BmiCalculator {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter weight in pounds: ");
double weight = input.nextDouble();
System.out.print("Enter height in inches: ");
double height = input.nextDouble();
//validate inputs
if(height <=0 || weight <=0) {
System.out.println("The invalid height or weight, "
+ "height and weight should be a positive non zero inputs");
return;
}
Bmi b = new Bmi();
double bmi = b.calculateBMI(weight, height);
String category = b.bmiCategory(bmi);
System.out.println("You belong to "+category+ " category");
}
}
**
输出:
输入体重(磅):100
输入高度(以英寸为单位):6
您属于肥胖类别
输入重量(磅):0
输入高度(以英寸为单位):6
无效身高或体重,身高和体重应为正非零输入