我正在尝试构建一个基本的重量计算器并遇到“变量 weightOut 可能尚未初始化”错误。谷歌似乎表明这是因为我需要初始化 "weightOut" 而不是仅仅设置它 " = weightIn * .78 " 。这是真的?这是我的代码。
Scanner keyboard = new Scanner(System.in);
int weightIn, planetNumber;
double weightOut;
System.out.print("What is your weight? ");
weightIn = keyboard.nextInt();
System.out.println("I have information for the following planets: ");
System.out.println(" 1. Venus ");
...
System.out.print("What planet are you going to? ");
planetNumber = keyboard.nextInt();
if( planetNumber == 1 )
{
weightOut = weightIn * 0.78;
}
...
System.out.println("Your weight would be " + weightOut +
" on that planet.");