2016-11-11 43 views
-4

我想知道我如何跳过我再试一次如果我的输入'输入'是无效的。例如,我输入'o'作为我的选择。它应该显示“无效的输入”,它应该显示菜单部分(再次跳过这个尝试)。请帮帮我。我需要跳过我的代码中的一部分,我不知道如何

public class Menu { 

    public static void MainMenu() { 
     Part1 call1 = new Part1(); 
     Part2 call2 = new Part2(); 
     Part3 call3 = new Part3(); 
     Part4 call4 = new Part4(); 

     Scanner in = new Scanner(System.in); 
     String yn = null; 


     do { 
      System.out.println("\t\t---HOMEWORK---"); 
      System.out.println("\tI for PART 1"); 
      System.out.println("\tII for PART 2"); 
      System.out.println("\tIII for PART 3"); 
      System.out.println("\tIV for PART 4"); 
      System.out.print("\tEnter input:  "); 
      String input = in.next(); 

      do { 
       switch (input) { 

        case "I": 
         call1.one(); 
         break; 

        case "II": 
         call2.two(); 
         break; 

        case "III": 
         call3.three(); 
         break; 

        case "IV": 
         call4.four(); 
         break; 

        case "V": 
         System.exit(0); 
         break; 

        default: 
         System.out.println("invalid input"); 
         break; 

       } 

       System.out.print("try again? -Y- || -N-  : "); 
       yn = in.next(); 

      } while (yn.equalsIgnoreCase("y")); 

     } while (yn.equalsIgnoreCase("n")); 
    } 

} 
+1

像'if(inputWasValid){...}'? – SomeJavaGuy

+0

我想我想说的是,如果我输入了一个无效的输入,它不会让用户选择再试一次。它会显示--HOMEWORK--部分,用户将输入另一个选择 –

+0

@ Ms.Smoak下面的问题值得接受吗? –

回答

0
public class Menu { 

public static void MainMenu() { 
    Part1 call1 = new Part1(); 
    Part2 call2 = new Part2(); 
    Part3 call3 = new Part3(); 
    Part4 call4 = new Part4(); 

    boolean inputWasValid = false; 

    Scanner in = new Scanner(System.in); 
    String yn = null; 


    do { 
     System.out.println("\t\t---HOMEWORK---"); 
     System.out.println("\tI for PART 1"); 
     System.out.println("\tII for PART 2"); 
     System.out.println("\tIII for PART 3"); 
     System.out.println("\tIV for PART 4"); 
     System.out.print("\tEnter input:  "); 
     String input = in.next(); 

     do { 
      switch (input) { 

       case "I": 
        call1.one(); 
        break; 

       case "II": 
        call2.two(); 
        break; 

       case "III": 
        call3.three(); 
        break; 

       case "IV": 
        call4.four(); 
        break; 

       case "V": 
        System.exit(0); 
        break; 

       default: 
        inputWasValid = true; 
        System.out.println("invalid input"); 
        break; 

      } 
      if (inputWasValid) { 
       break; 
      } 
      System.out.print("try again? -Y- || -N-  : "); 
      yn = in.next(); 

     } while (yn.equalsIgnoreCase("y")); 

    } while (yn.equalsIgnoreCase("n")); 
} 

}

为@Kevin说,你可以试试这个。

0

我的理解是,你想是这样的:

... 
    Scanner in = new Scanner(System.in); 
    String yn = null; 
    boolean retry; 


    do { 
     System.out.println("\t\t---HOMEWORK---"); 
     System.out.println("\tI for PART 1"); 
     System.out.println("\tII for PART 2"); 
     System.out.println("\tIII for PART 3"); 
     System.out.println("\tIV for PART 4"); 
     System.out.print("\tEnter input:  "); 

     String input = in.next(); 
     retry = true; 

     switch (input) { 

      ... 

      default: 
       System.out.println("invalid input"); 
       break; 
     } 

     System.out.print("try again? -Y- || -N-  : "); 
     yn = in.next(); 

     // might want to do check & loop here to see if user enters just Y or N 

     if(retry && yn.equalsIgnoreCase("N")) retry = false; 

    } while (retry); 

有了这个,你得到如下结果:

  • 输入I再试Y将通过循环再次运行
  • 输入I再试N将终止循环
  • 输入P再试Y将通过循环再次运行(无效的输入显示,但给用户选择继续的)
  • 输入P再试N将终止循环(显示无效的输入,用户决定不再继续)
相关问题