英文:
While loop goes infinite when moving back an iteration
问题
使用while循环提示用户输入3个整数以计算它们的平均值,当输入不是整数时需要重新提示,所以我决定在输入不是整数时在循环中退回一步,但是当我输入非整数时,似乎始终满足条件认为它不是整数,并且继续重新提示,而不重新检查新输入。
Scanner scnr = new Scanner(System.in);
String prompt = "输入一个整数:";
int num = 0;
int i = 0;
while (i < 3) {
System.out.print(prompt);
if (scnr.hasNextInt()) {
int input = scnr.nextInt();
num += input;
} else i -= 1;
i += 1;
}
double average = num / 3.0;
System.out.println("平均值:" + average);
英文:
Using a while loop to prompt the user to enter 3 ints to average them out, need to reprompt when the input isn't an int, so I decided to take a step back in the loop when the input isn't an int, but when I enter a non int, it's as if it consistently goes to the condition that it isn't a int, and continues to reprompt, without rechecking for a new input.
Scanner scnr = new Scanner(System.in);
String prompt = "Type an integer: ";
int num = 0;
int i = 0;
while (i < 3) {
System.out.print(prompt);
if (scnr.hasNextInt()) {
int input = scnr.nextInt();
num += input;
} else i -= 1;
i += 1;
}
double average = num / 3.0;
System.out.println("Average: " + average);
答案1
得分: 1
hasNextInt()
只有在已经有一个整数存在时才会返回true
,它实际上并不获取输入。这就是你对nextInt()
的调用所做的。但由于你从未实际获取用户输入,所以hasNextInt()
始终为假,因此在else
代码块中对i
进行了递减,然后再无限地递增。
另一种替代方法是使用带有nextInt()
的try/catch块来获取下一个输入值,如果捕获到异常(意味着输入不是整数),则退回。
英文:
hasNextInt()
only returns true
if an int is already there -- it doesn't actually get input. That's what your call to nextInt()
is doing. But that's never being called because hasNextInt()
is always false, as you've never actually taken user input, so i
is being decremented in the else
block and then incremented again forever.
An alternative approach would be to use a try/catch block with nextInt()
to get the next input value, and step back if that catches an exception (meaning the input was not an int).
答案2
得分: 0
由于先前的用户提到的主要问题是hasNextInt()
只进行检查并且不使您的扫描器向前移动。
以下是我会对这段代码进行的修改,希望可以解决这个问题:
int counter = 0;
boolean flag = false;
int sum = 0;
while(!flag){
System.out.println(prompt);
if (scnr.hasNextInt()){
int input = Integer.parseInt(scnr.nextLine());
sum = sum + input;
counter++;
}else{
scnr.nextLine();
System.out.println("您没有输入整数。");
}
if (counter == 3){
flag = true;
}
}
double avg = sum / 3.0;
System.out.println("平均值:" + avg);
可能还有更好的方法来实现,但我希望这对您有所帮助,祝您好运!
英文:
As a previous user has mentioned the main problem is that the hasNextInt() only checks and doesn't advance your scanner forward.
Here is how I would do this code, I hope it solves the issue:
int counter = 0;
boolean flag = false;
int sum = 0;
while(!flag){
System.out.println(prompt);
if (scnr.hasNextInt()){
int input = Integer.parseInt(scnr.nextLine());
sum = sum + input;
counter++;
}else{
scnr.nextLine();
System.out.println("You didn't enter an Integer.");
}
if (counter == 3){
flag = true;
}
}
double avg = sum / 3;
System.out.println("Average: " + avg);
There are probably better ways of doing it than mine but I hope it helps, good luck!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论