2014-03-05 71 views
0

我想檢查兩個整數輸入是否在範圍內並且是合法的。下面的代碼就是我想到的。檢查兩個整數輸入是否在範圍內 - java

問題是,當我檢查第一個輸入是否在範圍內時,它不會彈出一個錯誤(如果不正確),直到我輸入了第二個輸入,無論這是否正確。當錯誤出現時,它只是輸出錯誤以及等待用戶輸入的空行(無明顯原因);我在其中輸入任何內容並點擊「輸入」,然後程序再次運行(繼續循環),要求用戶再次輸入值直到它是正確的。

當我輸入一個字符時,不會發生此問題。輸入不匹配的錯誤完美工作。它會打印錯誤,然後再次運行該程序,要求用戶再次輸入值直至其合法。

我怎樣才能得到它來檢查第一個輸入,看看它是否在範圍內,並輸出一個錯誤,如果用戶點擊輸入後不正確,然後檢查第二個輸入,並做同樣的事情?

這是否有意義?

public String totalTime(){ 

    flag = 1; 

    do{ 
     try{ 
      System.out.print("Enter a starting destination: "); 
      int choice1 = user_input.nextInt(); 

      System.out.print("Enter a final desination: "); 
      int choice2 = user_input.nextInt(); 

      if((choice1 >= 1 && choice1 <= 5) && (choice2 >= 1 && choice2 <= 5)){ 
       switch(choice1){ 

        case 1: if (choice2 == 2){hour = 0; minutes = 10;} else if (choice2 == 3){hour = 0; minutes = 30;} else if (choice2 == 4) {hour = 1; minutes = 10;} else if (choice2 == 5) {hour = 1; minutes = 5;} break; 
        case 2: if (choice2 == 1){hour = 0; minutes = 10;} else if (choice2 == 3){hour = 0; minutes = 25;} else if (choice2 == 4) {hour = 1; minutes = 0;} else if (choice2 == 5) {hour = 0; minutes = 15;} break; 
        case 3: if (choice2 == 1){hour = 0; minutes = 48;} else if (choice2 == 2){hour = 0; minutes = 23;} else if (choice2 == 4) {hour = 0; minutes = 45;} else if (choice2 == 5) {hour = 0; minutes = 12;}break; 
        case 4: if (choice2 == 1){hour = 1; minutes = 5;} else if (choice2 == 2){hour = 1; minutes = 0;} else if (choice2 == 3) {hour = 0; minutes = 45;} else if (choice2 == 5) {hour = 0; minutes = 40;}break; 
        case 5: if (choice2 == 1){hour = 0; minutes = 30;} else if (choice2 == 2){hour = 0; minutes = 15;} else if (choice2 == 3) {hour = 0; minutes = 10;} else if (choice2 == 4) {hour = 0; minutes = 40;}break; 
        default: System.out.println("There is no such route"); 

       } 
       flag = 2; 
      } 
      else if (choice1 < 1 || choice1 > 5 || choice2 < 1 || choice2 > 5){ 
       throw new NumberFormatException("Integer is out of range."); 
      } 
     } 
     catch(NumberFormatException e){ 

      System.out.println("The number is not between 1 and 5. Try again."); 
      System.out.println(); 
      user_input.next(); //removes leftover stuff from input buffer 


     } 
     catch(InputMismatchException e){ 

      System.out.println("This is not an integer. Try again."); 
      System.out.println(); 
      user_input.next(); //removes leftover stuff from input buffer 

     } 
    }while (flag == 1); 
    return ("The total time is " + hour + " hours and " + minutes + " minutes"); 
} 
+0

你的意思是在catch塊? –

+0

OK so user_input.nextLine();在catch塊中彈出錯誤後停止隨機空行。但是我仍然想知道如何在每次輸入後打印錯誤... –

+0

您詢問第一個輸入,然後在不檢查對錯的情況下詢問第二個輸入。只有在此之後,你才檢查數值是否在範圍內。您的程序完全按照您的要求進行。 –

回答

0

要驗證並重新詢問用戶輸入,您需要在while循環中詢問輸入,並且不要離開循環直到輸入有效。

所以要求第一個輸入後立即,你可以這樣做

System.out.println("Enter a starting destination: "); 
     int choice1 = user_input.nextInt(); 

while(choice1 < 1 || choice1 > 5) { 
    System.out.println("Invalid Input"); 
    if(user_input.hasNextInt()) 
     choice1 = user_input.nextInt(); 
    else 
     user_input.next(); 
} 

System.out.println("Enter a final desination: "); 
     int choice2 = user_input.nextInt(); 

while(choice2 < 1 || choice2 > 5 || choice2 == choice1) { 
    System.out.println("Invalid Input"); 
    if(user_input.hasNextInt()) 
     choice2 = user_input.nextInt(); 
    else 
     user_input.next(); 
} 

這樣也避免了拋出,僅用於控制程序流程不必要的錯誤(可以是非常非常難讀)

相關問題