2013-11-20 32 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); 
} 
相关问题