我有这个程序,我想知道如何结束嵌套循环的内部循环。具体来说,如果用户选择不继续投掷硬币,如何返回内部循环(选择“t”)并将其返回到外部循环?我已经截断了部分代码,但我不明白如何返回主循环。
*{
Scanner anotherScanner = new Scanner(System.in);
boolean usersSelection = false;
String c;
while (!usersSelection)
{
System.out.println(""
+ "Selection: ");
if (anotherScanner.hasNext("q|Q"))
{
c = anotherScanner.next();
usersSelection = true;
System.out.println("you have selected to quit. If you wish to resume, reboot the program.");
break;
}
if (anotherScanner.hasNext("t|T"))
{
c = anotherScanner.next();
usersSelection = true;
Scanner obtain = new Scanner(System.in);
System.out.println("Please enter the number of coin flips");
int numero = obtain.nextInt();
if (numero > 0) {
for (int i = 0; i < numero; i++) {
int alpha = (int) (Math.random() * 2);
int beta = (int) (Math.random() * 2);
System.out.println(alpha + " " + beta);
}
}
System.out.println("Would you like to continue? Please enter yes or no.");
String response = obtain.next();
if(response.equalsIgnoreCase("y") || response.equalsIgnoreCase("yes")) {System.out.print("oh yeah");}
if(response.equalsIgnoreCase("no") || response.equalsIgnoreCase("n")) {break;}
else
{
String boom = obtain.next();
System.out.println("You have entered an invalid option. '"+boom+"' is not a valid option.");
}}
}*
答案 0 :(得分:0)
在循环前放置一个标签:
outer:
while (!usersSelection) {
// blah blah blah
}
然后将break;
替换为break outer;
。