InputMismatchException时

InputMismatchException时

本文介绍了InputMismatchException时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下为什么我会收到一个 InputMismathException



我已经声明了一个类型为 double 的变量,当我分配一个点值时,例如(4.6)它会抛出我:

 线程main中的异常java.util.InputMismatchException 
在java.util.Scanner.throwFor( Scanner.java:909)
在java.util.Scanner.next(Scanner.java:1530)
在java.util.Scanner.nextDouble(Scanner.java:2456)
在练习.ComputingMeanAndStandartDeviation_5_21.main(ComputingMeanAndStandartDeviation_5_21.java:18)

这是代码:

 包练习; 

import java.util。*;

public class ComputingMeanAndStandartDeviation_5_21
{

public static void main(String [] args)
{
扫描仪输入=新的扫描仪。在);

double sum = 0;
double number = 1;
double counter = 1;
System.out.println(输入十个数字:);
while(counter< 10)
{
number = input.nextDouble();
sum + = number;
counter ++;
}

System.out.println(sum ++ number ++ counter);
double mean = sum / counter;
System.out.println(的意思是:+的意思);
}

}


解决方案

区域设置中的问题

  Locale.setDefault(Locale.US); 
扫描仪输入=新的扫描仪(System.in); (78.12)而不是,(78.12)
$ b

p>

I would like to ask why I am getting an InputMismathException?

I have declared a variable of type double and when I assign it a point value e.g.(4.6) it throws me:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)
    at Exercises.ComputingMeanAndStandartDeviation_5_21.main(ComputingMeanAndStandartDeviation_5_21.java:18)

Here is the code:

package Exercises;

import java.util.*;

public class ComputingMeanAndStandartDeviation_5_21 
{

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

        double sum = 0;
        double number = 1;
        double counter = 1;
        System.out.println("Enter ten numbers: ");
        while(counter<10)
        {
            number = input.nextDouble();
            sum +=number;
            counter ++;
        }

        System.out.println(sum + "   " + number + " " + counter);
        double mean = sum / counter;
        System.out.println("The mean is: " + mean);
    }

}
解决方案

Problem in locale

Locale.setDefault(Locale.US);
Scanner input = new Scanner(System.in);

US decimal delimiter "."(78.12) and not ","(78,12)

这篇关于InputMismatchException时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:52