gpt4 book ai didi

java - 为什么我不断收到错误: cannot find symbol for variables “weight” and “planet” ?

转载 作者:行者123 更新时间:2023-12-02 11:08:23 25 4
gpt4 key购买 nike

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);
}

由于某种原因无法识别变量 weightplanet,并且基本上无法识别此代码中的所有其他变量。我以为这最初是扫描程序问题,但是在两种情况下都使用了 input.nextInt();,并且 input被声明为 Scanner

最佳答案

至少在您发布的代码中未声明它们。

改成 :

System.out.println("Enter weight: ");
int weight = input.nextInt();

System.out.println("Which planet: ");
int planet = input.nextInt();

关于java - 为什么我不断收到错误: cannot find symbol for variables “weight” and “planet” ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28666650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com