在为项目制作文本游戏时遇到嵌套切换语句的问题。我知道这可能不是最有效的方法,但我必须使用嵌套开关。 ' a1'值是程序中先前定义的字符串,主要是'是一个三行字符串,在播放每个案例后为玩家提供三种移动方式。以下是嵌套开关的一部分以及错误代码。好像我在第5行收到错误。抱歉,如果格式不正确。
switch (aplha) {
case 'a': System.out.println(""); System.out.println(a1); System.out.println(Main); System.out.println(""); char bravo = J.next().charAt(0);
switch (bravo) {
case 'a': System.out.println(""); System.out.println(a11); System.out.println(Main); System.out.println(""); char echo = J.next().charAt(0);
switch (echo) {
case 'a': System.out.println(""); System.out.println(a111); System.out.println(Main); System.out.println(""); char november = J.next().charAt(0);
switch (november) {
case 'a': System.out.println(""); System.out.println(a1111); System.out.println(Main); System.out.println(""); char oscar1 = J.next().charAt(0);
switch (oscar1) {
case 'a': System.out.println(""); System.out.println(a11111); break;
case 'b': System.out.println(""); System.out.println(a11112); break;
case 'c': System.out.println(""); System.out.println(a11113); break;
}
case 'b': System.out.println(""); System.out.println(a1112); System.out.println(Main); System.out.println(""); char pilot1 = J.next().charAt(0);
switch (pilot1) {
case 'a': System.out.println(""); System.out.println(a11121); break;
case 'b': System.out.println(""); System.out.println(a11122); break;
case 'c': System.out.println(""); System.out.println(a11123); break;
}
case 'c': System.out.println(""); System.out.println(a1113); System.out.println(Main); System.out.println(""); char quebec1 = J.next().charAt(0);
switch (quebec1) {
case 'a': System.out.println(""); System.out.println(a11131); break;
case 'b': System.out.println(""); System.out.println(a11132); break;
case 'c': System.out.println(""); System.out.println(a11133); break;
}
}
case 'b': System.out.println(""); System.out.println(a112); System.out.println(Main); System.out.println(""); char oscar = J.next().charAt(0);
switch (oscar) {
case 'a': System.out.println(""); System.out.println(a1121); System.out.println(Main); System.out.println(""); char romeo1 = J.next().charAt(0);
switch (romeo1) {
case 'a': System.out.println(""); System.out.println(a11211); break;
case 'b': System.out.println(""); System.out.println(a11212); break;
case 'c': System.out.println(""); System.out.println(a11213); break;
}
case 'b': System.out.println(""); System.out.println(a1122); System.out.println(Main); System.out.println(""); char sierra1 = J.next().charAt(0);
switch (sierra1) {
case 'a': System.out.println(""); System.out.println(a11221); break;
case 'b': System.out.println(""); System.out.println(a11222); break;
case 'c': System.out.println(""); System.out.println(a11223); break;
}
case 'c': System.out.println(""); System.out.println(a1123); System.out.println(Main); System.out.println(""); char tiny1 = J.next().charAt(0);
switch (tiny1) {
case 'a': System.out.println(""); System.out.println(a11231); break;
case 'b': System.out.println(""); System.out.println(a11232); break;
case 'c': System.out.println(""); System.out.println(a11233); break;
}
}
case 'c': System.out.println(""); System.out.println(a113); System.out.println(Main); System.out.println(""); char pilot = J.next().charAt(0);
switch (pilot) {
case 'a': System.out.println(""); System.out.println(a1131); System.out.println(Main); System.out.println(""); char umbrella1 = J.next().charAt(0);
switch (umbrella1) {
case 'a': System.out.println(""); System.out.println(a11311); break;
case 'b': System.out.println(""); System.out.println(a11312); break;
case 'c': System.out.println(""); System.out.println(a11313); break;
}
case 'b': System.out.println(""); System.out.println(a1132); System.out.println(Main); System.out.println(""); char victor1 = J.next().charAt(0);
switch (victor1) {
case 'a': System.out.println(""); System.out.println(a11321); break;
case 'b': System.out.println(""); System.out.println(a11322); break;
case 'c': System.out.println(""); System.out.println(a11323); break;
}
case 'c': System.out.println(""); System.out.println(a1133); System.out.println(Main); System.out.println(""); char whiskey1 = J.next().charAt(0);
switch (whiskey1) {
case 'a': System.out.println(""); System.out.println(a11331); break;
case 'b': System.out.println(""); System.out.println(a11332); break;
case 'c': System.out.println(""); System.out.println(a11333); break;
}
}
}
case 'b': System.out.println(""); System.out.println(a12); System.out.println(Main); System.out.println(""); char foxtrot = J.next().charAt(0);
switch (foxtrot) {
case 'a': System.out.println(""); System.out.println(a121); System.out.println(Main); System.out.println(""); char quebec = J.next().charAt(0);
switch (quebec) {
case 'a': System.out.println(""); System.out.println(a1211); System.out.println(Main); System.out.println(""); char xim1 = J.next().charAt(0);
switch (xim1) {
case 'a': System.out.println(""); System.out.println(a12111); break;
case 'b': System.out.println(""); System.out.println(a12112); break;
case 'c': System.out.println(""); System.out.println(a12113); break;
}
case 'b': System.out.println(""); System.out.println(a1212); System.out.println(Main); System.out.println(""); char yankee1 = J.next().charAt(0);
switch (yankee1) {
case 'a': System.out.println(""); System.out.println(a12121); break;
case 'b': System.out.println(""); System.out.println(a12122); break;
case 'c': System.out.println(""); System.out.println(a12123); break;
}
case 'c': System.out.println(""); System.out.println(a1213); System.out.println(Main); System.out.println(""); char zulu1 = J.next().charAt(0);
switch (zulu1) {
case 'a': System.out.println(""); System.out.println(a12131); break;
case 'b': System.out.println(""); System.out.println(a12132); break;
case 'c': System.out.println(""); System.out.println(a12133); break;
}
}
直接通过案例' a'时的错误代码是:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - duplicate case label
at javatester.JavaTester.main(JavaTester.java:415)
Java返回:1 建筑失败(总时间:3秒)
答案 0 :(得分:0)
好的,所以你的代码有点乱,但我认为你的错误是因为你有多个case'a','b'和'c'在同一个交换机的某个地方。您不能在同一个switch语句中重复一个案例。
编辑:你也必须为每个开关都有一个休息时间,即使是外部开关,这实际上就是为什么它认为你重复了相同的情况。