扫描仪方法 .hasNextInt() 和 if 语句 - 仅工作 1 次。对于下一个循环 - 自动(不等待任何输入)给出 false

Scanner method .hasNextInt() and if statement - works only 1 time. For next loop - automatically (didn't wait any input) gives false

我尝试进行输入检查(需要使用扫描仪获取 3 个数字)。 在此之前,我在另一项任务中使用了类似的方法 (.hasNext(int)) - 一切正常。在这种情况下,它不起作用。 第一个“while”循环正常工作,在第二个循环中 .hasNextInt() returns false 并循环 - 没有机会输入数据。

boolean num1IsInt = false;
boolean num2IsInt = false;
boolean num3IsInt = false;
int scaicius1 = 0;
int scaicius2 = 0;
int scaicius3 = 0;

System.out.println("Įveskite 3 skaičiai, po viena after each press enter");

while (!num1IsInt) { //check first number is int
    Scanner sc1 = new Scanner(System.in);
    System.out.println("(1)Įveskyte pirmas skaicius");
    if (sc1.hasNextInt()) {
        scaicius1 = sc1.nextInt();
        num1IsInt = true;
    } else {
        System.out.println("Not correct integer");
        continue;
    }
    sc1.close();
}

while (!num2IsInt) { //check second number is int
    Scanner sc2 = new Scanner(System.in);
    System.out.println("(2)Įveskyte antras skaicius");
    if (sc2.hasNextInt()) {
        scaicius2 = sc2.nextInt();
        num2IsInt = true;
    } else {
        System.out.println("Not correct integer");
        continue;
    }
    sc2.close();
}

while (!num3IsInt) { //check third number is int
    Scanner sc3 = new Scanner(System.in);
    System.out.println("(3)Įveskyte trecias skaicius");
    if (sc3.hasNextInt()) {
        scaicius3 = sc3.nextInt();
        num3IsInt = true;
        sc3.close();
    } else {
        System.out.println("Not correct integer");

        continue;
    }
    sc3.close();
}

System.out.println("First number = " + scaicius1);
System.out.println("First number = " + scaicius2);
System.out.println("First number = " + scaicius3);

谢谢 - @Thomas Kläger。我像开始时那样更改代码(只有一个扫描仪,因为有 3 个扫描仪它不起作用)并为这个“循环我的代码的幽灵元素”添加到所有其他语句 reader。

        boolean num1IsInt = false;
        boolean num2IsInt = false;
        boolean num3IsInt = false;
        int scaicius1 = 0;
        int scaicius2 = 0;
        int scaicius3 = 0;

        System.out.println("Įveskite 3 skaičiai, po viena after each press enter");
        **Scanner sc = new Scanner(System.in);**

        while (!num1IsInt) { //check first number is int
            System.out.println("(1)Įveskyte pirmas skaicius");
            if (sc.hasNextInt()) {
                scaicius1 = sc.nextInt();
                num1IsInt = true;
            } else {
                System.out.println("Not correct integer");
                **sc.next();**
            }
        }

        while (!num2IsInt) { //check second number is int
            System.out.println("(2)Įveskyte antras skaicius");
            if (sc.hasNextInt()) {
                scaicius2 = sc.nextInt();
                num2IsInt = true;
            } else {
                System.out.println("Not correct integer");
                sc.next();
            }
        }

        while (!num3IsInt) { //check third number is int
            System.out.println("(3)Įveskyte trecias skaicius");
            if (sc.hasNextInt()) {
                scaicius3 = sc.nextInt();
                num3IsInt = true;
            } else {
                System.out.println("Not correct integer");
                sc.next();
            }
        }
        sc.close();

        System.out.println("First number = " + scaicius1);
        System.out.println("First number = " + scaicius2);
        System.out.println("First number = " + scaicius3);