Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试输入是否为整数时如何避免无止境循环_Java_Input - Fatal编程技术网

Java 测试输入是否为整数时如何避免无止境循环

Java 测试输入是否为整数时如何避免无止境循环,java,input,Java,Input,我试图只允许整数输入,并在输入其他内容时阻止程序中断。不知何故,我的代码创建了一个无止境的循环,不允许我输入新的输入 private static int x; public static void main(String[] args) { testInput(); } public static void testInput(){ while (true){ System.out.println("P

我试图只允许整数输入,并在输入其他内容时阻止程序中断。不知何故,我的代码创建了一个无止境的循环,不允许我输入新的输入

    private static int x;

    public static void main(String[] args) {
        testInput();
    }

    public static void testInput(){
        while (true){
            System.out.println("Please enter Integer:");
            try{
                setX(scanner.nextInt());
                break;
            }catch (InputMismatchException i){
                System.out.println("Please use Integer");
            }
        }
    }

    public static void setX(int integer){
        x = integer;
    }
}

创建无限循环,循环内容为:请输入整数:,请使用整数,而不是让我进行新的输入。

使用while循环是正确的,基本上,当用户输入整数以外的内容时,您只需使用:

    private static int x;

    public static void main(String[] args) {
        testInput();
    }

    public static void testInput(){
        while (true){
            System.out.println("Please enter Integer:");
            try{
                setX(scanner.nextInt());
                break;
            }catch (InputMismatchException i){
                System.out.println("Please use Integer");
            }
        }
    }

    public static void setX(int integer){
        x = integer;
    }
}
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            String prompt = "Please enter an integer: ";
            int userInteger = getIntegerInput(prompt, scanner);
            System.out.printf("You entered: %d%n", userInteger);
        }
    }

    private static int getIntegerInput(String prompt, Scanner scanner) {
        System.out.print(prompt);
        int validInteger = -1;
        while (scanner.hasNext()) {
            if (scanner.hasNextInt()) {
                validInteger = scanner.nextInt();
                scanner.close();
                break;
            } else {
                System.out.println("Error: Invalid input, try again...");
                System.out.print(prompt);
                scanner.next();
            }
        }
        return validInteger;
    }
}
用法示例:

请输入一个整数:a
错误:输入无效,请重试。。。

请输入一个整数:我确定这是重复的,但基本上
scanner.nextInt()
不会使用非整数的令牌。我个人倾向于完全避免
扫描仪
,而只是从
系统中读取行。我看到很多关于
扫描仪的问题,基本上表明它是一个难以使用的API。相关但不是dup: