所以我的问题是这会在运行的同时,一旦您输入一个,它将仅打印下一个开关并结束程序,而不允许输入下一个选项。到目前为止,这是我所做的。
关于我正在做什么的任何提示都会有所帮助。
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);
}
}
答案 0 :(得分:0)
在条件允许的情况下尝试使用break内部
示例:
if(choice == '4')
break;
答案 1 :(得分:0)
System.exit(0);
break;
可以写为
return;
答案 2 :(得分: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.");
choice = in.next().charAt(0);
break;
在此行中,再次要求用户输入内容,然后应用程序终止。
答案 3 :(得分:0)
那么您的代码会执行什么操作,它会打印出带有“请进行选择”和3个选项的第一个文本块。然后,您获取用户输入(因此'1'
)。
它输入第一个switch语句并进入case '1'
。它打印出所需的内容并中断该switch语句。
然后进入while (choice == '1')
循环。在这里,您无需等待并获取新输入即可立即输入第二个switch case
。
因为您不等待获取新的输入,所以choice
的值仍为'1'
。
因此,在第二个switch case
中,您立即输入case '1'
。
在这种情况下,它将打印需要打印的内容。然后,您脱离该switch case
,脱离while (choice == '1')
循环,最后脱离do while
循环。