Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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 为什么即使在使用hasNextInt()方法时,我也会得到inputMismatchException?_Java_Inputmismatchexception - Fatal编程技术网

Java 为什么即使在使用hasNextInt()方法时,我也会得到inputMismatchException?

Java 为什么即使在使用hasNextInt()方法时,我也会得到inputMismatchException?,java,inputmismatchexception,Java,Inputmismatchexception,此程序要求用户输入其学生id号,该编号必须是0-999999之间的整数。注释掉的testID方法使用一个do while循环和一个内部while循环,确保用户只输入整数值。这种方法没有任何问题。 每次运行程序并键入字符串或字符值时,试图重写代码(第二个testID方法),我都会得到一个inputmaschException。第一种方法不会发生这种情况。有人能解释一下为什么会这样吗 import java.util.*; public class StudentID{ public st

此程序要求用户输入其学生id号,该编号必须是0-999999之间的整数。注释掉的testID方法使用一个do while循环和一个内部while循环,确保用户只输入整数值。这种方法没有任何问题。 每次运行程序并键入字符串或字符值时,试图重写代码(第二个testID方法),我都会得到一个
inputmaschException
。第一种方法不会发生这种情况。有人能解释一下为什么会这样吗

import java.util.*;
public class StudentID{

    public static int studentID= -1;
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args){
        testID();
    }

    /*
    public static void testID(){
        System.out.println("Enter your Student EMPLID (0-999999):");
        do{
            while (!input.hasNextInt()){
                input.nextLine();
                System.out.println("Enter a valid Student EMPLID (0-999999).");
            }
            studentID = input.nextInt();
            if(0 > studentID || studentID > 999999){
                input.nextLine();
                System.out.println("Enter a valid Student EMPLID (0-999999).");
            }
        } while (0 > studentID || studentID > 999999);
        System.out.println("Student EMPLID: " + studentID);
    }

    */

    public static void testID(){
        System.out.println("Enter your Student EMPLID (0-999999:)");
        while ((!input.hasNextInt()) && (0 > studentID) && (studentID > 999999)){
            input.nextLine();
            System.out.println("Enter a valid Student EMPLID (0-999999:)");
        }
        studentID = input.nextInt();
        System.out.println("Student EMPLID: " + studentID);
    }   

}

问题在于
while
中的逻辑。在注释的
testID()
方法中,您检查了以下条件为真:

while(!input.hasNextInt()) {
    ....
}
因此,对于非整数输入,
input.hasNextInt()
将返回
false
!hasNextInput()
将返回
true
,而
将继续循环,直到输入有效的
整数

现在在案例2中,
while
中的条件总是false

while(!input.hasNextInt()) && (0 > studentID) && (studentID > 999999) {
    ...
}
请参阅,此处的
studentID
默认设置为
-1
,因此即使
!input.hasNextInt()
按预期返回
true
,与
(studentID>999999)
and
true
的结果为
false
。因此,代码将永远不会进入
循环,并移动到下一行

studentID = input.nextInt();

这将抛出一个
输入不匹配异常
,因为输入的值不是
整数

对我来说很好。您正在输入什么?
(0>studentID)和&(studentID>999999)
将始终为false。您确定要进行此检查吗?对于第一个testID方法,我可以输入“五”,hasNextInt()方法将进行检查以确保它是一个int,如果不是,它将打印“输入有效…”,然后使用nextLine()重置扫描仪。对于新的一个,如果我输入“五”,我会得到一个inputMismatchException;没有初始化它,第一个testID方法就工作了。我只设置studentID=-1;因为我认为它永远不会进入循环,因为默认值是0。我将其更改回默认值(0),但仍然得到一个异常。@BamBamBondy如果
studentID
设置为
0
,该条件仍将失败,因为
studentID>999999
仍将
false
。问题出在逻辑上。例如,在第二种情况下,如何设置
studentID
?在第二种情况下,用户无法设置
studentID
。在第一种方法中,首先验证输入是
int
,然后继续检查该数字是否在允许的范围内。第二种情况对你正在尝试做的事情不会“按原样”起作用。案例1很完美,谢谢。我没有考虑如何验证输入,然后在第一种情况下设置变量。我将使用第一种方法。