BMI计算器。怎么了?

时间:2014-06-07 13:22:13

标签: java

我只是想知道如何删除初始化的wgt错误。休息,我希望我的代码基本而且非常简单。

//Program By Aryansh Malviya
//BMI Calculator
import java.util.Scanner;

public class BMICalc
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        int cw;
        int ch;
        int bmi;
        int wgt;
        int hgt;
        int ct;

        System.out.print("\nNote: This program is not yet fully functional.\nThere might be issues regarding decimal values. \nProgram has not been equipped to accept or display in decimal values. \nThe program will be updated soon. \nSorry for the inconvinience.");
        System.out.print("Enter 1 if you want weight in Kilograms");
        System.out.print("Enter 2 if you want weight in Pounds");
        cw = input.nextInt();

        System.out.print("\nNote: If you choose to enter weight in Pounds, you'd have to enter height in Inches. Else, in meters");
        if(cw == 1)
        {
            System.out.print("\nEnter weight in Kilograms: ");
            wgt = input.nextInt();
        }

        else if(cw == 2)
        {
            System.out.print("\nEnter weight in Pounds: ");
            wgt = input.nextInt();
        }

         else
         {
            System.out.print("\nEnter a valid choice and try again!");
             }

        System.out.print("\nEnter your height: ");
        hgt = input.nextInt();

    bmi = wgt/(hgt * hgt);

    System.out.printf("\nYour weight is: %d", wgt);
    System.out.printf("\nYour height is: %d", hgt);
    System.out.printf("\n\nYour BMI is: %d", bmi);

    System.out.print("\nBMI VALUES");
    System.out.print("\nUnderweight: less than 18.5");
    System.out.print("\nNormal: between 18.5 and 24.9");
    System.out.print("\nOverweight: between 25 and 29.9");
    System.out.print("\nObese: 30 or greater");
}
}

当我编译程序时,我得到变量wgt尚未初始化的错误。请告诉我如何解决这个问题。

3 个答案:

答案 0 :(得分:0)

您有两个if语句 - 但如果用户既未输入1也未输入2,则最终会出现在您尝试使用wgt的部分代码中一直在初始化它。

你应该做两件事:

首先,在第一次声明它时初始化wgt(这会使编译器错误消失)。

int wgt=0; 

会这样做。

接下来,当cw既不是1也不是2时,请确保您的程序陷阱。您可以使用带有标志的while循环或其他一些机制。现在,cw的错误输入将继续(您打印"输入有效选择"但实际上并没有回到开头......)

以下是解决所有问题的方法:

import java.util.Scanner;

public class BMICalc
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        int cw=0;
        int ch;
        double bmi;
        double wgt=0;
        double hgt;
        int ct;
        double BMIinchPoundFactor = 703;

        System.out.print("\nNote: This program is not yet fully functional.\nThere might be issues regarding decimal values. \nProgram has not been equipped to accept or display in decimal values. \nThe program will be updated soon. \nSorry for the inconvinience.");

        System.out.print("\nNote: If you choose to enter weight in Pounds, you'd have to enter height in Inches. Else, in meters");
        boolean validInput = false;
        while (!validInput) {
          System.out.print("\nEnter 1 if you want weight in Kilograms");
          System.out.print("\nEnter 2 if you want weight in Pounds\n");
          cw = input.nextInt();
          switch(cw) {
          case 1:
            System.out.print("\nEnter weight in Kilograms: ");
            wgt = input.nextDouble();
            validInput=true;
            break;
          case 2:
            System.out.print("\nEnter weight in Pounds: ");
            wgt = input.nextDouble();
            validInput=true;
            break;
          default:
            System.out.print("\nEnter a valid choice and try again!");
          }
        }
        System.out.print("\nEnter your height: ");
        hgt = input.nextDouble();

        bmi = wgt/(hgt * hgt);

        if(cw==2) bmi *= BMIinchPoundFactor;

        System.out.printf("\nYour weight is: %.0f", wgt);
        System.out.printf("\nYour height is: %.2f", hgt);
        System.out.printf("\n\nYour BMI is: %.1f", bmi);

        System.out.print("\nBMI VALUES");
        System.out.print("\nUnderweight: less than 18.5");
        System.out.print("\nNormal: between 18.5 and 24.9");
        System.out.print("\nOverweight: between 25 and 29.9");
        System.out.print("\nObese: 30 or greater\n\n");
    }
}

我采取了一些其他的自由:

  1. double用于可能不是整数值的内容
  2. 清理I / O(添加一些'\n'
  3. 创建一个循环,一直持续到您提供有效输入
  4. 以磅和英寸为单位正确计算BMI
  5. 我确信它可以进一步改善......

答案 1 :(得分:0)

  bmi = wgt/(hgt * hgt);

您在这里使用wgt,如果cw既不是1也不是2,则可能无法初始化。

答案 2 :(得分:0)

无法保证wgt已初始化。例如,用户可以在第一个问题中输入“3”。你应该考虑和处理非法输入。