【发布时间】:2018-10-10 13:43:21
【问题描述】:
所以我的问题是,当它运行时,一旦你输入一个,它只会打印下一个开关并结束程序,而不允许输入下一个选项。这是我到目前为止所做的。
- 我已将 while(choice == 1) 放入案例 1 中。
- 我已经采取了 while(choice == 1) 并且只是使用了开关 在 switch 和使用 continue 语句中。
- 当我取出最后两次休息时,我得到了 无限循环。
任何关于我正在做什么的提示都会有所帮助。
package Spook;
import java.util.Scanner;
public class House {
Scanner in = new Scanner(System.in);
static void p(String I) {
System.out.println(I);
}
public void game() {
p("\nWelcome To Spook House, were all Spooks will haunt you.");
do {
p("\nPlease make your selection");
p(" 1. Enter House for some Scares.");
p(" 2. Too Scared to Enter.");
p(" 3. Really Scared Please let me exit.");
p("Choose one please.");
char choice = in.next().charAt(0);
switch (choice) {
case '1':
p("\nAs prepare to enter the house, The door slowly creaks open.");
p("\nYou enter the house and the door slams shut.");
p("What do you do????");
p(" 1. Try to open the door.");
p(" 2. Find the nearist closet and hide.");
p(" 3. Continue onward.");
p(" 4. Faint and end game.");
break;
case '2':
p("\nWhat are you a chicken, Just press 1!!!!!");
break;
case '3':
p("\nFine you win chicken, now ending.");
System.exit(0);
break;
}
while (choice == '1') {
switch (choice) {
case '1':
p("\nAs you twist the door knoob and try to pull it open. You feel a gust of wind that pushes you down.");
break;
case '2':
p("\nYou run towards the closet hoping to hide till daylight.");
p("You start to shake and laugh nerviously.");
break;
case '3':
p("\nYou explore the first room.");
p("You see an old crooked picture of a scary clown.");
break;
case '4':
p("\nYou have been easily to SPOOKED MMMMUUUUHHHHAAAAHHAAAA!!!!!");
System.exit(0);
break;
}
break;
}
break;
} while (true);
}
}
【问题讨论】:
-
请修正您的代码格式。
-
只是一件小事,“都是”应该是“在哪里”。 “准备进入”应为“准备进入”。 “最近的”应该是“最近的”。 “紧张”应该是“紧张”。 :-)
-
你真的试过调试你的代码吗?连续 3 行中的 3 次中断闻起来很麻烦。
-
提示:查看你的中断语句。将它们与你的循环和开关关联起来,看看哪个 break 语句中断了。
-
感谢那些评论。
标签: java switch-statement do-while