java 在要求输入两次时执行
java do while asked for input twice
为什么要输入2次?请帮助我是 java 的新手,找不到错误
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);
我删除了 do while 但它仍然要求第二个输入
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;
}
您应该将 System.out.println("Enter your age: ");
语句移到 do-while 循环之外。
您好 :D 因为 !in.hasNextInt() 会导致您需要再次输入,但您可以将其更改为其他条件,例如年龄大于特定值。
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);
}
}
它要求您输入两次的原因是您 in.hasNextInt()
将检查您的扫描仪是否有来自系统的输入。由于您的系统由于您调用 age = in.nextInt();
而没有任何输入,这会将扫描仪移动到您的 in.hasNextInt()
之前的下一个单词,函数 in.hasNextInt()
将要求您输入一些内容,以便它可以验证它是否是 Int。
我们要做的是首先检查当前扫描仪的输入是否有整数,然后再将其存储在 age 中或再次循环并请求新输入。
更好的检查方法是做这样的事情。
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);
}
}
输出:
Enter your age:
boy
Please enter a valid age:
boy girl
Please enter a valid age:
5
Your age is: 5
我已经更新了你的代码。这对你有用。
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
.
为什么要输入2次?请帮助我是 java 的新手,找不到错误
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);
我删除了 do while 但它仍然要求第二个输入
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;
}
您应该将 System.out.println("Enter your age: ");
语句移到 do-while 循环之外。
您好 :D 因为 !in.hasNextInt() 会导致您需要再次输入,但您可以将其更改为其他条件,例如年龄大于特定值。
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);
}
}
它要求您输入两次的原因是您 in.hasNextInt()
将检查您的扫描仪是否有来自系统的输入。由于您的系统由于您调用 age = in.nextInt();
而没有任何输入,这会将扫描仪移动到您的 in.hasNextInt()
之前的下一个单词,函数 in.hasNextInt()
将要求您输入一些内容,以便它可以验证它是否是 Int。
我们要做的是首先检查当前扫描仪的输入是否有整数,然后再将其存储在 age 中或再次循环并请求新输入。
更好的检查方法是做这样的事情。
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);
}
}
输出:
Enter your age:
boy
Please enter a valid age:
boy girl
Please enter a valid age:
5
Your age is: 5
我已经更新了你的代码。这对你有用。
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
.