【问题标题】:How do I check whether user enters an integer between 2 numbers? [duplicate]如何检查用户是否输入了两个数字之间的整数? [复制]
【发布时间】:2016-04-26 12:24:32
【问题描述】:

我希望用户输入 80 到 120 之间的整数,没有字母和其他符号。这是我的代码:

import java.util.*;

public class Test {     

public static void main(String[] args)
{       
    Scanner in = new Scanner(System.in);

//checking for integer input
    while (!in.hasNextInt())
    {
        System.out.println("Please enter integers between 80 and 120.");
        in.nextInt();
        int userInput = in.nextInt();

//checking if it's within desired range
        while (userInput<80 || userInput>120)
        {
            System.out.println("Please enter integers between 80 and 120.");
            in.nextInt();
        }
    }
}

}

但是,我遇到了一个错误。有解决办法吗?

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Array.main(Array.java:15)

谢谢! :)

编辑:谢谢汤姆,得到了解决方案,但想尝试不使用“do”

    Scanner in = new Scanner(System.in);

    int userInput;
    do {
    System.out.println("Please enter integers between 80 and 120.");
    while (!in.hasNextInt())
    {
        System.out.println("That's not an integer!");
        in.next();
        }
        userInput = in.nextInt();
} while (userInput<81 || userInput >121);

System.out.println("Thank you, you have entered: " + userInput);
}
}

【问题讨论】:

  • 当然有! catchjava.util.InputMismatchException 并妥善处理。出于兴趣,您为什么要跳过输入?
  • @Bathsheba 嗨,我想不使用 catch 来做这件事,因为我正在修改我的学校作业,这只是前几个主题(此时还没有学习 catch 异常)
  • 将条件改为while (in.hasNextInt())

标签: java input integer


【解决方案1】:

您的循环条件错误。您检查诸如“只要输入中没有可用的整数:读取整数”之类的内容。会失败的

另外:你打电话给nextInt 两次。不要那样做。删除第一个调用:

System.out.println("Please enter integers between 80 and 120.");
in.nextInt(); //REMOVE THIS LINE
int userInput = in.nextInt();

您使用 hasNextInt 检查一次 int 是否可用,但随后您读取了两次值!

【讨论】:

  • 我试过了,还是一样的错误。
  • 是的,我尝试删除它,但仍然出现相同的错误 D:
  • while (!in.hasNextInt()) 也许你的问题就在这里。
  • 我认为你的问题是 while (!in.hasNextInt()) 因为你明确告诉它检查整数。
【解决方案2】:
    boolean continueOuter = true;

    while (continueOuter)
        {
            System.out.println("Please enter integers between 80 and 120.");
            String InputVal = in.next();

        try {
            int input = Integer.parseInt(InputVal);
              //checking if it's within desired range
             if(FirstInput>80 && FirstInput<120)
            {
               //no need to continue got the output
                continueOuter = false;
            }else{
                  System.out.println("Please enter integers between 80 and  120."); //need to continue didnt got the exact output
                 continueOuter = true;      
            } 

        } catch (Exception e) {
        //not an int    
       System.out.println(InputVal);
       continueOuter = true;
        } 
}

在这段代码中,我创建了一个布尔值来检查程序是否要继续。如果用户输入了一个有效值,程序将停止 但是,您可以根据需要更改它。你不需要两个while循环我已经将内部while循环更改为if循环看看我的代码

【讨论】:

  • continue = false?祝你在 Java 中好运。
  • 我指的是 continueouter 这是一个错误。
  • @priyamal 你好,如果用户输入一个字符怎么办? i 头部字符是指数字或其他东西。
  • 编辑了代码。如果不是 int,catch 块中的代码将被执行。如果输入是 int,try 块中的代码将被执行。
  • 你的意思是String ?? :D:D 抱歉,我没有使用 ide,非常抱歉您指出了这一点。
猜你喜欢
  • 1970-01-01
  • 2020-04-30
  • 2019-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多