Double break java
本问题已经有最佳答案,请猛点这里访问。
我用Java编程,我不知道如何使用Sturk;命令从一个以上的循环中退出。
下面是我尝试的一些代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | while (true){ System.out.println(" Bank Menu: What action do you want to perform(Enter a number)? 1. Create a bank account 2. Log in to a preexisting account 3. Exit bank system"); String action1 = scanner.next(); if (action1.equals("1")) { if(!(password==null)){ if(createAccountTracker==1){ createAccountTracker=0; break; } System.out.println("Are you sure? Creating a new account will remove your preexisting account 1.Yes 2.Cancel"); createaccount = scanner.next(); if(createaccount.equals(2)) { createAccountTracker=1; break; } } System.out.println("What will your account name be(One Word Only)?"); accName = scanner.next(); System.out.println("Please enter your account password:"); passwordCheck1 = scanner.next(); System.out.println("Please reenter your account password:"); passwordCheck2 = scanner.next(); if(passwordCheck1.equals(passwordCheck2)){ password= passwordCheck2; System.out.println("Your account has been made! Thank you for creating an account! Account name:" + accName +" Password:" + password ); accBalance=0; } else{ System.out.println("Error: Two passwords entered do not match. Please try again."); } } |
我试图这样做,如果他们输入2(取消),代码将打破两个循环,回到银行菜单。我看到的这个问题并没有真正帮助我理解。事先谢谢!
您可以这样从2个循环中分离出来:
1 2 3 4 5 6 7 8 | outer: while(condition) { // while or for loops, same way while(condition2) { break outer; // break out of the 2 loops } } // Other code |
"外部"标签标记了外部循环。