【发布时间】:2017-06-21 02:34:15
【问题描述】:
为学校作业做这件事,但对开关和案例的工作方式感到困惑。我以为我终于让它工作了,但我仍然收到格式错误,我不知道为什么。
import java.util.Scanner;
public class PartyAffiliation
{
public static void main(String[] args)
{
do
{
String Party = null;
boolean running = true;
Scanner in = new Scanner(System.in);
loopParty: while(running)
{
System.out.println("What is your politcal party? (D for Democrat - R for Republican - I for Independent");
Party = in.nextLine();
switch (Party)
{
case ("D"):
System.out.println("You get a Democratic Donkey!");
running=false;
break;
case ("R"):
System.out.println("You get a Republican Elephant!");
running=false;
break;
case ("I"):
System.out.println("You get an Independent Man!");
running=false;
break;
default:
System.out.println("I guess you aren't any of the three.");
break;
}
}
}
}
}
【问题讨论】:
-
什么是格式错误?
-
线程“main”java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的树类型:
在 PartyAffiliation.main。我不确定,我对 Java 不太了解。 -
这是一个编译错误,因为您的
do循环没有相应的while条件。如果您希望它无限循环,请将do替换为while(true)。 -
可能根本不想要
do, -
谢谢,看起来确实解决了问题。它确实会一直循环,这是我不想要的,但我会尝试解决这个问题。
标签: java switch-statement case