Java 什么';检查输入的数字是否为正整数并重新提示的最简单方法是什么?

Java 什么';检查输入的数字是否为正整数并重新提示的最简单方法是什么?,java,validation,Java,Validation,程序接受的用户输入应该是大于0的整数。如果用户不这样做,他会被告知错误并受到谴责。输入正确的输入后,返回值。最好的方法是什么?下面的代码是我的尝试,但不起作用。对于这样一项简单的任务来说,它似乎不必要地复杂 System.out.println("Please enter an integer greater than 0:"); Scanner scan = new Scanner(System.in); int red = -1; do {

程序接受的用户输入应该是大于0的整数。如果用户不这样做,他会被告知错误并受到谴责。输入正确的输入后,返回值。最好的方法是什么?下面的代码是我的尝试,但不起作用。对于这样一项简单的任务来说,它似乎不必要地复杂

    System.out.println("Please enter an integer greater than 0:");
    Scanner scan = new Scanner(System.in);
    int red = -1;
    do
    {
        try
        {
            red = scan.nextInt();
        }catch(InputMismatchException e)
        {
            System.out.println("Number must be an integer"); 
            scan.nextLine();
            if(red < 1)
                System.out.println("Number must be more than zero");
            else
                break;
        }
    }while(true);
    return red;
System.out.println(“请输入大于0的整数:”;
扫描仪扫描=新扫描仪(System.in);
红色整数=-1;
做
{
尝试
{
红色=scan.nextInt();
}捕获(输入不匹配异常e)
{
System.out.println(“数字必须是整数”);
scan.nextLine();
如果(红色<1)
System.out.println(“数字必须大于零”);
其他的
打破
}
}虽然(正确);
返回红色;

有时我不知道该在我的问题中添加什么,因为我已经知道代码不起作用-因此,如果有其他事情我应该告诉你,请让我知道。

基本概念是朝着正确的方向运行的,但是要小心,
nextInt
不会占用新行,将其留在扫描仪中,这意味着您将在第一个不成功的循环之后结束一个无限循环

就我个人而言,我只需要使用
nextLine
将输入作为
字符串
,这将占用新行,导致下一个循环在语句处停止

然后我将使用
Integer.parseInt将
字符串
解析为
int

例如

Scanner scan = new Scanner(System.in);
int red = -1;
do {
    System.out.print("Please enter an integer greater than 0:");
    String text = scan.nextLine();
    if (text != null && !text.isEmpty()) {
        try {
            red = Integer.parseInt(text);
            // This is optional...
            if (red < 1) {
                System.out.println("Number must be more than zero");
            }
        } catch (NumberFormatException exp) {
            // This is optional...
            System.out.println("Not a number, try again...");
        }
    }
} while (red < 1);
Scanner scan=新的扫描仪(System.in);
红色整数=-1;
做{
System.out.print(“请输入一个大于0的整数:”);
String text=scan.nextLine();
if(text!=null&&!text.isEmpty()){
试一试{
红色=整数.parseInt(文本);
//这是可选的。。。
如果(红色<1){
System.out.println(“数字必须大于零”);
}
}捕获(NumberFormatException exp){
//这是可选的。。。
System.out.println(“不是数字,请重试…”);
}
}
}红色<1;

我使用该类而不是
扫描仪
缓冲读取器
类来获取用户输入:

import java.io.*;
public class Input{
private static BufferedReader input=new BufferedReader
                (new InputStreamReader(System.in));
public static Double getDouble(String prompt){
    Double value;
    System.out.print(prompt);
    try{
        value=Double.parseDouble(Input.input.readLine());
    }
    catch (Exception error){
        // error condition
        value=null;
    }
    return value;
}
public static Integer getInteger(String prompt){
    Integer value;
    System.out.print(prompt);
    try{
        value=Integer.parseInt(Input.input.readLine());
    }
    catch (Exception error){
        // error condition
        value=null;
    }
    return value;
}
public static String getString(String prompt){
    String string;
    System.out.print(prompt);
    try{
        string=Input.input.readLine();
    }
    catch (Exception error){
        // error condition
        string=null;
    }
    return string;
}
}
现在,为了回答您的问题,您可以这样编写代码:

public class InputTest {


public int checkValue() {

    int value; 
    do {
        value = Input.getInteger("Enter a value greater than 0: ");
    } while (value <= 0);

    return value;

    }
   }
公共类输入测试{
公共int校验值(){
int值;
做{
value=Input.getInteger(“输入一个大于0的值:”);

}while(其中一个问题是
nextInt
不会使用新行字符,这意味着您将在第一次过去后进入无限循环…我不确定
scanner
是否;)