程序中的Java运行时错误InputMismatchException

问题描述 投票:0回答:3

当我运行此代码时,我在coderunner(一个提交代码用于学校作业的应用程序)中获取此代码

    Scanner scan = new Scanner(System.in);

    double maxn = -90;
    double maxs = 90;
    double maxe = 180;
    double maxw = -180;
    double lat = 0;
    double longa = 0;
    int x = 1;

    while (x != 0) {
        System.out.println("Please enter a latitude:");
        lat = scan.nextDouble();
        if (lat >= maxn && lat <= 90)
            maxn = lat;
        if (lat <= maxs && lat >= -90)
            maxs = lat;

        System.out.println("Please enter a longitude:");
        longa = scan.nextDouble();
        if (longa <= maxe && longa >= -180)
            maxe = longa;
        if (longa >= maxw && longa <= 180)
            maxw = longa;

        System.out.println("Would you like to enter another location?");
        x = scan.nextInt();

    }
    System.out.println("Farthest North: " + maxn + "\nFarthest South: " + maxs + "\nFarthest East: " + maxe + "\nFarthest West: " + maxw);

我收到以下错误:

    Runtime Error

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Lesson_20_Activity.main(Main.java:315)
at Ideone.assertRegex(Main.java:85)
at Ideone.assertRegex(Main.java:76)
at Ideone.test(Main.java:40)
at Ideone.main(Main.java:29)

我不知道这个错误是如何工作的,因为我不熟悉编码。有人可以解释这意味着什么以及如何解决它?

编辑:我的意见是

Please enter the latitude: 41.678 Please enter the longitude: 69.938 Would you like to enter another location? 1 Please enter the latitude: 41.755 Please enter the longitude: 69.862 Would you like to enter another location? 1 Please enter the latitude: 41.829 Please enter the longitude: 69.947 Would you like to enter another location? 1 Please enter the latitude: 300 Please enter the longitude: 69.947 Incorrect Latitude or Longitude Please enter the latitude: 41.827 Please enter the longitude: 69.904 Would you like to enter another location? 0 Farthest North: 41.829 Farthest South: 41.678 Farthest East: 69.947 Farthest West: 69.862

另外,我尝试将x更改为double和字符串输入,但没有成功。我得到的错误是NoSuchElementError和NoSuchLineError(分别)

java exception runtime-error inputmismatchexception
3个回答
1
投票

JavadocInputMismatchException投掷时:

由扫描程序抛出,表示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

在你的代码中,你正在调用scan.nextInt()scan.nextDouble()。确保您只分别向这些调用中传递有效的intdouble值。也就是说,当Scanner期望int值(scan.nextInt())时输入double值会引发上述错误。


0
投票

请注意,当您不想换行时,您应该使用System.out.print然后写'\ r \ n'。

我已经运行了您的代码并得到了正确的输出,但您必须注意您对命令行的输入。我读了所有字符串,然后转换为所需的原始格式,以避免此错误。


0
投票

输入经度和纬度后,您要问的是再次尝试的问题。

x= nextInt()

在这里,你接受一个int(0来关闭while循环,否则)。如果在那里键入任何字符串或double值,将导致Inputmismtach异常。我尝试了你的代码,如果正确地给出了这些整数和小数,它可以正常工作,如下所示。

Please enter a latitude:
12.3
Please enter a longitude:
14.3
Would you like to enter another location?
1
Please enter a latitude:
12.3
Please enter a longitude:
14.5
Would you like to enter another location?
0
Farthest North: 12.3
Farthest South: 12.3
Farthest East: 14.3
Farthest West: 14.5
© www.soinside.com 2019 - 2024. All rights reserved.