Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java在请求输入时执行两次_Java_Do While - Fatal编程技术网

java在请求输入时执行两次

java在请求输入时执行两次,java,do-while,Java,Do While,为什么我需要输入2次?请帮助我,我是java新手,找不到错误 do{ System.out.println(“输入您的年龄:”; 年龄=in.nextInt(); 如果(!in.hasNextInt()){ System.out.println(“请输入有效年龄”); in.nextInt(); 有效=错误; } }有效期; 我删除了do,但它仍然要求第二个输入 System.out.println(“输入您的年龄:”); 年龄=in.nextInt(); 如果(!in.hasNextInt(

为什么我需要输入2次?请帮助我,我是java新手,找不到错误

do{
System.out.println(“输入您的年龄:”;
年龄=in.nextInt();
如果(!in.hasNextInt()){
System.out.println(“请输入有效年龄”);
in.nextInt();
有效=错误;
}
}有效期;
我删除了do,但它仍然要求第二个输入

System.out.println(“输入您的年龄:”);
年龄=in.nextInt();
如果(!in.hasNextInt()){//将运行,直到找到整数输入
System.out.println(“请输入有效年龄”);
in.nextInt();
有效=错误;
}

您应该移动
系统.out.println(“输入您的年龄:”)语句在do while循环之外。

您好:因为!在.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);
        
          
    }
}

它两次要求您输入的原因是由于您在.hasNextInt()中执行了
操作,该操作将检查扫描仪是否有来自系统的输入。由于调用
age=in.nextInt(),系统没有任何输入
将扫描仪移动到.hasNextInt()中的
之前的下一个单词,.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 

说明:如果您提供的是有效数据,则循环将继续接受输入(不接受两次输入)。一旦您提供了无效数据,代码将提示您输入有效数据,循环将停止执行,因为您在发布代码之前是否尝试过此代码?我打赌你没有,因为它不会正常工作。(提示:您有
true
false
反转)我做了,您可以尝试运行…我得到了正确的输出。我的概念是,如果年龄大于90岁,则将再次关联用户输入,如果年龄大于90岁,则继续并打印输出oops。我道歉。你说得对。让我吃惊的是你使用了“valid”这个名字。这应该是“无效的”,对吗?我将while读取为
while(!valid)
。在用户输入正确的年龄之前,是否要获取年龄?如果用户未输入整数,我希望它重新输入有效年龄,则无需手动检查。如果用户没有在nextInt()中输入整数,则将抛出一个
inputmaschexeption
。你可以处理它。我需要代码来完成我的作业。@Jonathan2340这样行吗?嗨,我想你把int age=in.nextInt()放入do while循环的逻辑可能是错误的。试着连续输入2个错误的输入,比如“boy”,我不想在输入正确的输入后继续循环,但是谢谢anyway@Jonathan2340我已经更新了我的答案。现在,它将适用于所有场景。@Jonathan2340这样可以吗?希望我回答你的问题时能说明为什么我改变了某些事情。如果你觉得我的答案有帮助,请不要投赞成票