首页 > 解决方案 > Java BMI 程序

问题描述

代码的第一部分正在运行,程序将输入作为身高和体重,并显示输出消息。当我按 n 时,我可以进入第二部分并输入以英寸为单位的身高和以磅为单位的重量。我一拿到它们,程序就会输出结果 bmi 是:Nan。并且变量 bmi 可能尚未初始化。我想将身高和体重从英寸和磅转换为米,然后显示结果 bmi 这是我的代码:

package uly14th;
import java.util.Scanner;

public class BmiCalculator {

    private static float BMI2;
    public static void main(String[] args) {
        char Y; 
        char y; 
        char Q;
        char n;
        char N;
        float heightInMeters, weightInKilograms, BMI;
        float heightInInches, weightInpounds;
        float heightInMeters2 = 0, weightInKilograms2 = 0;
        
        Scanner input = new Scanner(System.in);
        System.out.println("Please state whether you are going to use kilograms & meters or, inches & pounds");
        System.out.println("If you want to use the former please press Y or if you want to use the latter please press N");
        Q = input.next().charAt(0);
        if(Q == 'y' || Q == 'Y' ) {
            System.out.println("Please enter the height in meters: ");
            heightInMeters = input.nextFloat();
            System.out.println(" and weight in kilograms: "); 
            weightInKilograms = input.nextFloat();
            
            BMI =  weightInKilograms / (heightInMeters * heightInMeters);
           
            System.out.println("The resulting BMI of the person is: " +BMI);
            if (BMI < 18.5){
                System.out.println("The personis underweight");
            }
            else if ((BMI<=25.0) && (BMI >= 18.5)){
                System.out.println("The person is normal");
            }
            else if ((BMI >= 25.0) && (BMI <=30.0 )){
                System.out.println("The personis obese");
            }
            else if(BMI>=30.0) {
                System.out.println("The person is obese and should exercise");
            }
        }
        if ((Q == 'n') || (Q == 'N')){
            System.out.println("Please enter the height in inches: ");
            
            heightInInches = input.nextFloat();
            System.out.println("Please enter the weight in pounds: ");
            weightInpounds = input.nextFloat();
            
            heightInInches = 0.0254f * heightInMeters2;
            weightInpounds = 0.453592f * weightInKilograms2;
          
            BMI =  weightInKilograms / (heightInMeters * heightInMeters);
            
            System.out.println("The resulting BMI of the person is: " +BMI); 
            
            if (BMI < 18.5){
                System.out.println("The personis underweight");
            }
            else if ((BMI<=25.0) && (BMI >= 18.5)){
                System.out.println("The person is normal");
            }
            else if ((BMI >= 25.0) && (BMI <=30.0 )){
                System.out.println("The personis obese");
            }
            else if(BMI>=30.0) {
                System.out.println("The person is obese and should exercise");
            }
        }
        else {
            System.out.println("Please make sure you run the program again and then only use the characters n,N,y,Y. ");
        }   
    }        
}

这是错误消息:线程“main”中的异常 java.lang.RuntimeException:无法编译的源代码 - 变量 BMI 可能未在 uly14th.BmiCalculator.main(BmiCalculator.java:82) 初始化

标签: java

解决方案


在行(第 82 行)

 BMI =  weightInKilograms / (heightInMeters * heightInMeters);

您使用了从未初始化过的变量weightInKilograms ,因此您的错误。heightInMeters

我怀疑您想在计算 BMI 之前从英寸和磅转换为米和公斤。您可以通过简单地将以下几行放在 BMI 计算之前来做到这一点

heightInMeters = 0.0254f * heightInInches;
weightInKilograms = 0.453592f * weightInPounds;

BMI =  weightInKilograms / (heightInMeters * heightInMeters); //Now you can calculate the BMI using your variables since they have been assigned a value and is therefore initialized

推荐阅读