javaoop中的NaN输出

javaoop中的NaN输出,java,oop,Java,Oop,我刚刚开始学习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 * he

我刚刚开始学习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");
        }            
    }
}
import java.util.Scanner;
类BIM
{
整数权重;
双倍重量kg=重量*0.45359237;
内部高度;
双高度表=高度*0.0254;
双倍体重指数=体重千克/(身高米*身高米);
public void calculateBMI()
{    
系统输出打印(“BMI为:“+BMI”);
}
}
公共类BMI
{
公共静态void main(字符串args[])引发异常
{
扫描仪输入=新扫描仪(System.in);
系统输出打印(“以磅为单位输入重量:”;
int-weight=input.nextInt();
系统输出打印(“以英寸为单位输入高度:”;
int height=input.nextInt();
BIM b=新的BIM();
b、 calculateBMI();
}
专用静态无效类别(双倍BMI)
{
如果(体重指数<18.5)
{
系统输出打印项次(“重量不足”);
}
否则,如果(体重指数<25.0)
{
系统输出打印项次(“正常”);
}
否则如果(体重指数<30.0)
{
系统输出打印项次(“超重”);
}
其他的
{
System.out.println(“肥胖”);
}            
}
}
我的代码的输出:

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 体重指数为:NaN


为什么我会得到输出?还有,为什么不显示类别?

错误1
您没有为BMI类的对象设置
weight
height
属性的值。您需要在BMI类中添加以下代码:

BMI(体重、身高){
重量=重量;
高度=高度;
}
然后创建一个BMI类的对象作为

BMI b = new BMI(weight, height)
错误2

您没有在任何地方调用
bmicography
函数,因此该类别不会显示。

您的
BIM
类完成所有计算,但您从未向其传递
身高或
体重(在任何地方分配这些值之前,您都会进行计算)。首先,修复
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:双倍体重指数=体重千克/(身高米*身高米)

您试图将weightKG除以零,请参见下面的内联注释

**

**

问题2:您从未调用方法bmicography来打印基于BMI的分类

如果我要重新格式化并重新编写代码,我会这样做。我们仍然可以通过异常处理来改进这一点,但至少我们可以做到这一点

**

import java.util.Scanner;
类体重指数{
公共双计算器BMI(双倍重量,双倍高度){
双倍重量kg=重量*0.45359237;
双高度表=高度*0.0254;
双倍体重指数=体重千克/(身高米*身高米);
返回体重指数;
}
公共字符串类别(双bmi){
如果(体重指数<18.5){
返回“减持”;
}
如果(体重指数<25.0){
返回“正常”;
}
如果(体重指数<30.0){
返回“超重”;
}
返回“肥胖”;
}
}
公共类BMI计算器{
公共静态void main(字符串参数[]){
扫描仪输入=新扫描仪(System.in);
系统输出打印(“以磅为单位输入重量:”;
双倍权重=input.nextDouble();
系统输出打印(“以英寸为单位输入高度:”;
双倍高度=input.nextDouble();
//验证输入

如果(身高)在当前代码中计算一次。计算时,
height
0
weight
0
。您输入的主要方法的局部体重和身高,BIM中的身高和体重为0和NaN(不是数字)被零除的结果是否不完全正确,体重和身高是0。我如何正确地调用BMI类别?我主要通过执行-->BMIControle();或BMIControle(双倍BMI);或BMIControly(BMI)来调用BMIControle,但根据您发布的代码,
new BMI()。BMIControle(b.BMI)
-重构发布的代码将是一个好主意。这很有效。感谢您的清晰解释。这对像我这样刚刚开始学习的初学者非常有帮助yesterday@BellaSwan我学Java已经24年了。练习。
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");

  }


}