当我输入 double 时,nextDouble() 抛出一个 InputMismatchException

发布于 2024-11-05 23:01:00 字数 936 浏览 2 评论 0原文

import java.util.*;

class Averager
{
    public static double unlimited()
    {
        int count = 0;
        double sum = 0;
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext())
        {
            double d = scan.nextDouble();
            sum += d;
            count++;
        }
        double ave = sum/count;
        return ave;
    }

    public static void main(String[] args) {
        System.out.println(unlimited()+"\n");
    }
}

当我使用整数时没有错误,但如果我使用带有点的数字,则会出现错误。

$ javac Averager.java; java Averager
0.5
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at Averager.unlimited(Averager.java:12)
    at Averager.main(Averager.java:21)

据我所知,0.5 应该被双倍覆盖。如果没有请有人纠正我。

import java.util.*;

class Averager
{
    public static double unlimited()
    {
        int count = 0;
        double sum = 0;
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext())
        {
            double d = scan.nextDouble();
            sum += d;
            count++;
        }
        double ave = sum/count;
        return ave;
    }

    public static void main(String[] args) {
        System.out.println(unlimited()+"\n");
    }
}

There is no error when I use integers but if I use numbers with point in it a error appears.

$ javac Averager.java; java Averager
0.5
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at Averager.unlimited(Averager.java:12)
    at Averager.main(Averager.java:21)

To my best understanding 0.5 should be covered by double. If not please can someone correct me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2024-11-12 23:01:00

它可能取决于区域设置。例如,十进制数在瑞典写为 0,5。

更改您的代码,使其显示例如:

Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);

It might be locale dependent. Decimal numbers are e.g written as 0,5 in Sweden.

Change your code so that it says e.g.:

Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);
夏见 2024-11-12 23:01:00
This worked for me, changing the locale did not.

  Scanner sc = new Scanner(System.in);
  // val = sc.nextDouble(); - crashes with java.util.NoSuchElementException
  // If Java crashes with legal Java code, wrap the call in a hasNextLine() test
  if (sc.hasNextLine())
  {
    val = sc.nextDouble();
  }

​​java.util.NoSuchElementException:找不到行

This worked for me, changing the locale did not.

  Scanner sc = new Scanner(System.in);
  // val = sc.nextDouble(); - crashes with java.util.NoSuchElementException
  // If Java crashes with legal Java code, wrap the call in a hasNextLine() test
  if (sc.hasNextLine())
  {
    val = sc.nextDouble();
  }

java.util.NoSuchElementException: No line found

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文