import java.io.*;
import java.util.*;
public class Planets
{
public static void main(String[] args) throws IOException
{
Scanner in;
in = new Scanner(System.in);
boolean runAgain = true;
System.out.println("Starting program...nn");
double weight = getWeight();
while(runAgain)
{
String planet = getPlanet();
calculateWeight(weight, planet);
System.out.println("nWould you like to see your weight on a different planet? (Yes or No)");
String input = in.nextLine();
if(input.charAt(0)=='N' || input.charAt(0)=='n')
{
runAgain = false;
}
}
} //end main
public static double getWeight() throws IOException
{ //Prompts user to enter their weight
Scanner in;
in = new Scanner(System.in);
System.out.println("Greetings! This program calculates how much you would weigh on other planets.nnPlease enter your weight (lbs.): ");
double weight = in.nextDouble();
return weight;
}
public static String getPlanet() throws IOException
{ //Prompts user to enter their planet
Scanner in;
in = new Scanner(System.in);
System.out.println("nChoose your planet:nSunnMoonnSaturnnMercurynMarsnUranusnVenusnJupiternNeptune");
String planet = in.nextLine();
return planet;
}
public static void calculateWeight(double weight, String planet) throws IOException
{ //Outputs the weight of the given planet ; takes two parameters weight and planet
switch(planet.charAt(0))
{
case 's':
case 'S':
switch(planet.charAt(1))
{
case 'u':
case 'U':
System.out.println("Your weight on the Earth is " + weight + " lbs.nYour weight on the Sun is " + 27.94*weight);
break;
case 'a':
case 'A':
System.out.println("Your weight on the Earth is " + weight + " lbs.nYour weight on Saturn is " + .91*weight);
break;
}
case 'm':
case 'M':
switch(planet.charAt(1))
{
case 'o':
case 'O':
System.out.println("Your weight on the Earth is " + weight + " lbs.nYour weight on the Moon is " + .17*weight);
break;
case 'e':
case 'E':
System.out.println("Your weight on the Earth is " + weight + " lbs.nYour weight on Mercury is " + .37*weight);
break;
case 'a':
case 'A':
System.out.println("Your weight on the Earth is " + weight + " lbs.nYour weight on Mars is " + .38*weight);
break;
}
case 'u':
case 'U':
System.out.println("Your weight on Earth is " + weight + " lbs.nYour weight on Uranus is " + .88*weight);
break;
case 'v':
case 'V':
System.out.println("Your weight on Earth is " + weight + " lbs.nYour weight on Venus is " + .90*weight);
break;
case 'j':
case 'J':
System.out.println("Your weight on Earth is " + weight + " lbs.nYour weight on Jupiter is " + 2.64*weight);
break;
case 'n':
case 'N':
System.out.println("Your weight on Earth is " + weight + " lbs.nYour weight on Neptune is " + 1.12*weight);
break;
}
}
} //end class
正在做CS101课的作业。我们的教授希望我们使用嵌套的switch语句和charAt来打印我们在特定星球上的体重,以确定用户正在谈论的是哪个星球。嵌套的switch语句没有按预期执行。
错误:当我选择Sun时,它输出Sun和Uranus的结果
当我选择月亮时,它输出月亮和天王星的结果
当我选择土星时,它输出土星,火星和天王星的结果。
当我选择水星时,它输出水星和天王星的结果
当我选择Mars时,它输出Mars和Mercury的结果。
我不明白为什么要打印多个行星的结果,即使索引处的字母不匹配。天王星、金星、木星和海王星工作得很好,和预期的一样。
您需要在嵌套的switch
之后添加break
语句,以便您将break
从外部的switch
中取出:
switch(planet.charAt(0))
{
case 's':
case 'S':
switch(planet.charAt(1))
{
case 'u':
case 'U':
...
break;
}
break; // this one is needed.
case 'm':
...
或者你可以标记switch
语句,然后标记break outer;
。
在每个case
块的内容之后,您要将break
取出。你的内的switch的case
块可以做到这一点,但你的外部没有。在内部的switch
的右括号之后和下一个case
之前添加一个break;
。
由于缺少break;
,您直接滚动到下一个case
块。