英文:
java do while asked for input twice
问题
为什么我需要输入两次?请帮忙,我是 Java 新手,找不到错误。
原代码:
do {
System.out.println("请输入您的年龄:");
age = in.nextInt();
if (!in.hasNextInt()) {
System.out.println("请输入有效的年龄");
in.nextInt();
valid = false;
}
} while (valid);
我去掉了 do while 循环,但它仍然要求第二次输入。
修改后代码:
System.out.println("请输入您的年龄:");
age = in.nextInt();
if (!in.hasNextInt()) { // 会一直运行直到输入一个整数
System.out.println("请输入有效的年龄");
in.nextInt();
valid = false;
}
英文:
why do I need to input 2 times? please help I'm new to java, can't find the error
do {
System.out.println("Enter your age: ");
age= in.nextInt();
if(!in.hasNextInt()) {
System.out.println("Please enter a valid age");
in.nextInt();
valid=false;
}
}while(valid);
I removed the do while but it still asks for the second input
System.out.println("Enter your age: ");
age= in.nextInt();
if(!in.hasNextInt()) { //Will run till an integer input is found
System.out.println("Please enter a valid age");
in.nextInt();
valid=false;
}
答案1
得分: 2
我已经更新了你的代码。这对你来说将是有效的。
public class Example
{
public static void main(String[] args) {
boolean valid = true;
do {
Scanner in = new Scanner(System.in);
System.out.println("Enter your age: ");
while(!in.hasNextInt()) {
System.out.println("Please enter a valid age");
in.next();
valid = false;
}
int age = in.nextInt();
} while(valid);
}
}
输出:
Enter your age:
2
Enter your age:
seven
Please enter a valid age
seven
Please enter a valid age
3
解释:如果你提供了有效的数据,循环将继续接受输入(不会重复接受输入)。一旦你提供了无效的数据,代码将提示你输入有效的数据,循环将停止执行,因为你将 valid = false
。
英文:
I have updated your code. This will work for you.
public class Example
{
public static void main(String[] args) {
boolean valid = true;
do {
Scanner in = new Scanner(System.in);
System.out.println("Enter your age: ");
while(!in.hasNextInt()) {
System.out.println("Please enter a valid age");
in.next();
valid = false;
}
int age = in.nextInt();
} while(valid);
}
}
Output :
Enter your age:
2
Enter your age:
seven
Please enter a valid age
seven
Please enter a valid age
3
Explanation : If you are giving valid data, then the loop will continue to take inputs (not taking inputs twice). As soon as, you give invalid data, then, the code will prompt you to enter the valid data and loop will stop executing as you made valid = false
.
答案2
得分: 1
以下是翻译的内容:
它要求您两次输入的原因是由于您的代码中的 in.hasNextInt()
,它将检查扫描器是否从系统中获取了输入。由于您调用了 age = in.nextInt();
,这会在检查 in.hasNextInt()
之前将扫描器移到下一个单词,所以函数 in.hasNextInt()
将要求您输入一些内容,以便验证它是否为整数。
我们想要做的是,在将输入存储在年龄变量中或重新循环并要求新的输入之前,先检查当前扫描器的输入是否为整数。
更好的检查方式是这样的:
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int age = 0;
System.out.println("请输入您的年龄:");
while (!in.hasNextInt()) {
System.out.println("请输入有效的年龄:");
in.nextLine();
}
age = in.nextInt();
System.out.println("您的年龄是:" + age);
}
}
输出:
请输入您的年龄:
男孩
请输入有效的年龄:
男孩 女孩
请输入有效的年龄:
5
您的年龄是:5
英文:
The reason why it asks you for input twice is due to you in.hasNextInt()
which will check your scanner for input from the system. Since your system does not have any input due to you calling age = in.nextInt();
which will move the scanner to the next word before your in.hasNextInt()
, The function in.hasNextInt()
will require you to input something so that it can validate if it is an Int or not.
what we want to do, is to first check the current scanner's input if it has an integer before we either store it inside age or loop again and ask for new input.
A better way of checking would be to do something like this.
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int age = 0;
System.out.println("Enter your age: ");
while(!in.hasNextInt()){// checks if scanner's next input is an int, return true if next input is not an Int and the while loop continues till the next input is an Int
System.out.println("Please enter a valid age: ");
in.nextLine();//move the scanner to receive the next nextLine
//this is important so the hasNextInt() wont keep checking the same thing
}
//it will only exit the while loop when user have successfully enter an interger for the first word they inputted.
age = in.nextInt();
System.out.println("Your age is: " + age);
}
}
Output:
Enter your age:
boy
Please enter a valid age:
boy girl
Please enter a valid age:
5
Your age is: 5
答案3
得分: 0
你应该将System.out.println("请输入您的年龄:");
语句移到do-while循环之外。
英文:
You should move the System.out.println("Enter your age: ");
statement outside the do-while loop.
答案4
得分: 0
以下是您提供的代码的翻译部分:
public class stackTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int age = 0;
boolean valid = false;
do {
System.out.println("输入您的年龄:");
age = in.nextInt();
if (age > 90) {
System.out.println("请输入一个有效的年龄");
valid = true;
} else
valid = false;
} while (valid);
System.out.println("年龄:" + age);
}
}
英文:
Hi : D there is becuase of the !in.hasNextInt() it will cause you need to do the input again but you can change it to other condition like if the age is bigger than certain value.
public class stackTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int age =0 ;
boolean valid = false;
do {
System.out.println("Enter your age: ");
age= in.nextInt();
if(age>90) {
System.out.println("Please enter a valid age");
valid=true;
}
else valid=false;
}while(valid);
System.out.println("Age: " + age);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论