public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double weight;
double newWeight;
int planet;
System.out.println("1. Mercury");
System.out.println("2. Venus");
System.out.println("3. Earth");
System.out.println("4. Mars");
System.out.println("5. Jupiter");
System.out.println("6. Saturn");
System.out.println("7. Uranus");
System.out.println("8. Neptune");
System.out.println("9. Pluto");
System.out.println(" ");
System.out.println("Select a planet from above: ");
planet = input.nextInt();
System.out.println("Enter your weight in pounds: ");
weight = input.nextInt();
switch(newWeight) {
case 1: (3.724 / 9.8) * weight = newWeight; break;
case 2: (8.918 / 9.8) * weight = newWeight; break;
case 3: (9.8 / 9.8) * weight = newWeight; break;
case 4: (3.724 / 9.8) * weight = newWeight; break;
case 5: (24.892 / 9.8) * weight = newWeight; break;
case 6: (10.584 / 9.8) * weight = newWeight; break;
case 7: (8.918 / 9.8) * weight = newWeight; break;
case 8: (11.662 / 9.8) * weight = newWeight; break;
case 9: (1.622 / 9.8) * weight = newWeight; break;
}
System.out.print("Your weight on " + planet + "is: " + newWeight);
}
编辑*****:确定没错,重量是一个int而newWeight是一个双倍。但现在是最后一行"System.out.print("your weight on " + planet + "is: " + newWeight);
它说"variable newWeight might not have been initialized".
如果不是一件事,那就是另一件事。
答案 0 :(得分:1)
赋值运算符的左侧应该是左值,而不是表达式,更改
(1.622 / 9.8) * weight = newWeight
到
newWeight = (1.622 / 9.8) * weight
答案 1 :(得分:1)
在我看来,您的weight
和newWeight
变量已被换掉。
首先,您尝试启用尚未设置值的newWeight
,然后您正在执行(3.724 / 9.8) * weight = newWeight
,这根本没有意义。
我想您想要启用planet
,然后使用
newWeight
newWeight = (3.724 / 9.8) * weight;
至于最后一行代码的错误,它正在发生,因为如果没有输入数字1到9,则newWeight为null。您可以添加默认大小写,也可以将所有变量初始化为0,如下所示:
int planet = 0;
double weight = 0.00, newWeight = 0.00;
答案 2 :(得分:1)
这里的错误比其他答案提到的更多:
您已将weight
声明为double
,但您要为其指定代码input.nextInt()
。应该是input.nextDouble()
?
作为其他答案:您的switch
应该在planet
上,即switch(planet)
作为其他答案,您的newWeight
作业已回到正面,例如newweight = (3.724 / 9.8) * weight
您尚未捕获用户为星球输入任何其他号码的情况。
您需要一个默认大小写,以确保已正确设置所有所需的变量。您的编辑可能会警告您这种可能性,default: newweight = 0
答案 3 :(得分:1)
好的,你们摇滚!让一切正常,除了地球的名称不会通过。相反,它显示了与该数字对应的行星的数字选择。但我会弄明白的。非常感谢你们的帮助!如果有人想知道他们在另一个星球上的重量是什么,这里是完成的代码:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double weight;
double newWeight;
int planet;
System.out.println("1. Mercury");
System.out.println("2. Venus");
System.out.println("3. Earth");
System.out.println("4. Mars");
System.out.println("5. Jupiter");
System.out.println("6. Saturn");
System.out.println("7. Uranus");
System.out.println("8. Neptune");
System.out.println("9. Pluto");
System.out.println(" ");
System.out.println("Select a planet from above: ");
planet = input.nextInt();
System.out.println("Enter your weight in pounds: ");
weight = input.nextDouble();
switch(planet) {
default: newWeight = 0.00;
case 1: newWeight = (3.724 / 9.8) * weight; break;
case 2: newWeight = (8.918 / 9.8) * weight; break;
case 3: newWeight = (9.8 / 9.8) * weight; break;
case 4: newWeight = (3.724 / 9.8) * weight; break;
case 5: newWeight = (24.892 / 9.8) * weight; break;
case 6: newWeight = (10.584 / 9.8) * weight; break;
case 7: newWeight = (8.918 / 9.8) * weight; break;
case 8: newWeight = (11.662 / 9.8) * weight; break;
case 9: newWeight = (1.622 / 9.8) * weight; break;
}
System.out.print("On " + planet + ", your new weight is: " + newWeight);
}
}
答案 4 :(得分:1)
首先,你应该将newWeight初始化为0.
double newWeight = 0;
和上面所说的一样,它应该是个案(星球)。
switch(planet) {
case 1: newWeight = (3.724 / 9.8) * weight; break;
case 2: newWeight = (8.918 / 9.8) * weight; break;
case 3: newWeight = (9.8 / 9.8) * weight; break;
case 4: newWeight = (3.724 / 9.8) * weight; break;
case 5: newWeight = (24.892 / 9.8) * weight; break;
case 6: newWeight = (10.584 / 9.8) * weight; break;
case 7: newWeight = (8.918 / 9.8) * weight; break;
case 8: newWeight = (11.662 / 9.8) * weight; break;
case 9: newWeight = (1.622 / 9.8) * weight; break;
}
也可以在最终的印刷语句中使用行星的名称而不是数字。也许把行星的所有名字都放到一个数组中。这是为了让你开始:)
ArrayList<String> planetName = new ArrayList(){{
add("Mercury");
add("Venus");
add("Earth");
add("Mars");
add("Jupiter");
add("Saturn");
add("Uranus");
add("Neptune");
add("Pluto");
}
};
for(int i = 0; i < planetName.size(); i++){
System.out.println((i+1) + ". "+ planetName.get(i) );
}
答案 5 :(得分:0)
不应该是switch(planet)
吗?否则你需要打开任意重量。
答案 6 :(得分:0)
您在switch
语句中放置了不正确的变量,而您的作业operators
则相反。
switch(planet) {
case 1: newWeight = (3.724 / 9.8) * weight; break;
//....
}