2013-02-01 85 views
-1

任何人都可以帮助我理解为什么程序需要我在继续之前输入我的选择两次?为什么我需要输入两次我的选择?

public static void main(String[] args) { 
    String quit = "quit"; 
    String input = "input"; 
    String print = "print"; 


    @SuppressWarnings("resource") 
    Scanner scan = new Scanner(System.in); 
    System.out.println("please enter a command : \n  1) Quit \n  2) Input \n  3) Print \n"); 

    String initialSelection = scan.next(); 
    boolean stringInput = scan.hasNext(); 
    boolean intInput = scan.hasNextInt(); 
    boolean boolinput = scan.hasNextBoolean(); 

    if(initialSelection.equalsIgnoreCase(quit)) { 
     System.out.println("The program has quit."); 
     System.exit(0); 
    }else if(initialSelection.equalsIgnoreCase(print)){ 
     [constructor] 

     do { 
      System.out.println("\nplease enter a command : \n  1) Quit \n  2) Input \n  3) Print \n"); 
      initialSelection = scan.nextLine(); 

       if (initialSelection.equalsIgnoreCase(print)) { 
         new BonusArrayList(); 
       } else if(initialSelection.equalsIgnoreCase(quit)) { 
          System.out.println("The program has quit."); 
          System.exit(0); 
       } 
     } while (initialSelection.equalsIgnoreCase(print)); 

    }else if(initialSelection.equalsIgnoreCase(input)){ 
     System.out.println("\n Please enter some kind of value (ex: 123, hello, True)"); 
    } 
} 

我相信它是从嵌套do-while语句来,但我似乎无法来解决这个问题。任何提示将不胜感激!

回答

0

这是因为你要么需要有

String initialSelection = scan.next(); 

 boolean stringInput = scan.hasNext(); 
     boolean intInput = scan.hasNextInt(); 
     boolean boolinput = scan.hasNextBoolean(); 

如果您同时需要输入两次

+0

这是有道理的。非常感谢! –

+0

如果您发现需要将其标记为正确答案 –

+0

,请再次致谢! –

相关问题