2014-02-17 3 views
0

У меня есть эта программа, и мне интересно, как я могу закончить внутреннюю петлю вложенного цикла. В частности, как я могу вернуть внутренний цикл (выбор «t») и вернуть его во внешний цикл, если пользователь выбирает не продолжать бросать монету? Я урезал часть своего кода, но я не понимаю, как вернуться в основной цикл.Как я могу выйти из цикла ijnterior вложенного цикла

*{  
     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

Поставьте метку перед циклом:

outer: 
while (!usersSelection) { 
    // blah blah blah 
} 

Затем замените break; с break outer;.

Смежные вопросы