我只想知道如何刪除初始化的wgt錯誤。休息,我希望我的代碼是基本的,非常簡單。BMI計算器。怎麼了?
//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沒有被初始化。請告訴我如何解決這個問題。
本地變量需要初始化,如錯誤消息所示。將它們初始化爲0. –
可能的重複[爲什麼局部變量未在Java中初始化?](http://stackoverflow.com/questions/415687/why-are-local-variables-not-initialized-in-java) – Niemand