Scanner input = new Scanner(System.in);
System.out.println("Enter weight: ");
weight = input.nextInt();
System.out.println("Which planet: ");
planet = input.nextInt();
int venus = 1;
int mars = 2;
int jupiter = 3;
int saturn = 4;
int uranus = 5;
int neptune = 6;
if (planet == 1)
{
venus_weight = weight * 0.78;
System.out.println(+ venus_weight);
}
else if (planet == 2)
{
mars_weight = weight * 0.39;
System.out.println(+ mars_weight);
}
else if (planet == 3)
{
jupiter_weight = weight * 2.56;
System.out.println(+ jupiter_weight);
}
else if (planet == 4)
{
saturn_weight = weight * 1.17;
System.out.println(+ saturn_weight);
}
else if (planet == 5)
{
uranus_weight = weight * 1.05;
System.out.println(+ uranus_weight);
}
else
{
neptune_weight = weight * 1.23;
System.out.println(+ neptune_weight);
}
变量weight
和planet
由于某种原因而未被识别,基本上是此代码中的所有其他变量。我认为这最初是扫描程序问题,但在这两种情况下都使用了input.nextInt();
,input
被声明为Scanner
。
答案 0 :(得分:2)
至少在您发布的代码中,它们未被声明。
更改为:
System.out.println("Enter weight: ");
int weight = input.nextInt();
System.out.println("Which planet: ");
int planet = input.nextInt();