[英]Scanner method .hasNextInt() and if statement - works only 1 time. For next loop - automatically (didn't wait any input) gives false
I tried to do an input check (need to take 3 numbers using Scanner).我尝试进行输入检查(需要使用扫描仪获取 3 个数字)。 Before that, I used a similar method (
.hasNext(int)
) in another task - everything worked fine.在此之前,我在另一项任务中使用了类似的方法(
.hasNext(int)
)——一切正常。 In this case, it doesn't work.在这种情况下,它不起作用。 The first "while" loop works correctly, on the second loop
.hasNextInt()
returns false and loops the loop - not giving the opportunity to enter data.第一个“while”循环正常工作,在第二个循环上
.hasNextInt()
返回 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);
Thank you - @Thomas Kläger.谢谢 - @Thomas Kläger。 I change code like was at the begin (with only one Scanner, cose with 3 scanners it's didn't work) and add to all else statements reader for this "ghost element which loop my code".
我像一开始一样更改代码(只有一个扫描仪,再加上 3 个扫描仪,它不起作用),并为这个“循环我的代码的幽灵元素”添加到所有其他语句阅读器。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.