2013-02-01 116 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

,請再次致謝! –

相關問題