2013-11-20 29 views
0

我想知道如何去驗證這段代碼,所以輸入只能是一個int,最小和最大之間?到目前爲止,我只能停止輸入小於1以及使用的最大值。但是我似乎無法創建一個場景,如果用戶輸入的內容不是循環的最大值和最小值之間的任何值(例如「AAA」)。我不斷收到輸入不匹配錯誤。任何幫助將不勝感激!如何實現int驗證,所以輸入只能是int和min和max之間的?

private static int getUserOption(String prompt, int max) { 
    int h; 
    do { 
     Scanner sc = new Scanner(System.in); 
     System.out.print(prompt); 
     h=sc.nextInt(); 
     if(!sc.hasNextInt()) { 
       System.out.println("Invalid option. Try again!:"); 
     } 
    } while(h<1 || h>max); 
    return h;  
} 
+0

可以給你輸入嗎? –

回答

0

您可以試試此代碼!

Scanner sc = new Scanner(System.in); 
int number; 
do { 
System.out.println("Please enter a valid number: "); 
while (!sc.hasNextInt()) { 
    System.out.println("Error. Please enter a valid number: "); 
    sc.next(); 
} 
number = sc.nextInt(); 
} while (!checkChoice(number)); 

private static boolean checkChoice(int choice){ 
if (choice <MIN || choice > MAX) {  //Where MIN = 0 and MAX = 20 
    System.out.print("Error. "); 
    return false; 
} 
return true; 
} 

Ref。 Validating input with while loop and scanner

1

由於nextInt()方法拋出一個異常,從而終止了方法,所以循環正在中斷。

一種選擇是使用一個try/catch塊套住異常:

private static int getUserOption(String prompt, int max) { 
    int h = 0; 
    do { 
     Scanner sc = new Scanner(System.in); 
     System.out.print(prompt); 
     try { 
      h=sc.nextInt(); 
     } catch (InputMismatchException e) { 
      System.out.println("Invalid option. Try again!:"); 
     } 
    }while(h<1 || h>max); 
    return h; 
} 
+0

謝謝,所以這隻適用於try/catch?我從來沒有使用過它,並希望在繼續之前變得精通做/做,做,做事。如果非常感謝您的幫助! – user2988501

+0

'sc.nextInt()'方法需要一個整數,所以如果你輸入一個字符串總是會拋出一個異常。你的另一個選擇是調用'sc.next()',它將以字符串的形式返回任何輸入;然後您可以檢查是否可以將其轉換爲整數,如果不打印錯誤消息。可以說更復雜:) –

0

幾點:

1.以檢查是否輸入小於一個最小值只需在方法簽名中添加int min即可。

2.檢查輸入是否爲int,catch InputMismatchException

修改後的代碼將是:

private static int getUserOption(String prompt, int max, int min) { 
    int h; 
    do { 
     Scanner sc = new Scanner(System.in); 
     System.out.print(prompt); 
     try { 
      h=sc.nextInt(); 
     } catch (InputMismatchException e) { 
      System.out.println("Invalid option. Try again!:"); 
     } 
    } while(h<min || h>max); 
    return h;  
} 
0

,如果你想繼續問,直到用戶鍵入一個有效的答案,或者只問一次,我無法理解。 while塊建議繼續詢問,直到輸入有效。 以下是您要求的小片段。我建議你閱讀一些書,在那裏有很多建議。

public static class InvalidUserException extends Exception { 

    public InvalidUserException(String message) { 
     super(message); 
    } 

    public InvalidUserException(String message, Throwable cause) { 
     super(message, cause); 
    } 

} 

private static int getIntFromScanner(int max) throws InvalidUserException { 
    int nextInt = 0; 

    Scanner sc = new Scanner(System.in); 
    try { 
     nextInt = sc.nextInt(); 

    } catch (InputMismatchException e) { 
     throw new InvalidUserException("Input must be a valid Integer", e); 
    } 

    if (nextInt > max) { 
     throw new InvalidUserException(
       "Input is bigger than allowed! Max: " + max + " Input: " 
         + nextInt); 
    } 
    return nextInt; 
} 

public static int getUserOption(String prompt, int max) { 
    System.out.println(prompt); 
    do { 
     try { 
      return getIntFromScanner(max); 
     } catch (InvalidUserException e) { 
      System.out.println("Invalid option. Try again (" 
        + e.getMessage() + ")"); 
     } 
    } while (true); 
} 

public static void main(String[] args) { 
    int userOption = getUserOption("Gimme less than or equal 6!", 6); 
    System.out.println("You gave me " + userOption); 
} 
相關問題