错误java.util.InputMismatchException

时间:2013-10-10 17:05:14

标签: java if-statement inputmismatchexception

我是Java新手,我正在尝试制作一个程序来查看一个人是否有正常的温度,而不是太低或太高。

我收到此错误消息:(当我输入一个double,而不是int)

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.nextDouble(Unknown Source)
        at ekstra217.main(ekstra217.java:15)

这是我的代码

import java.util.*;

class temp
{//klassen start
    public static void main(String[]args)
    {//main start


    Scanner tast=new Scanner(System.in);

    System.out.println("Write your temperatur!");
 //normal temperatur is between 36.5 and 37.5
    double temperatur=tast.nextDouble();
    if (temperatur<36.5)
    {
    System.out.println("Your temperatur is normal");
    } 
    else if(temperature>37.5)
    {//else if  starts
    System.out.println("You have over normal,you are sick");
    }//else if slutter

    else{
    System.out.println("You have normal temperature");
    }
}   
   } 

2 个答案:

答案 0 :(得分:3)

您似乎输入non-double值作为程序的输入,因此在执行时遇到InputMismatchException

tast.nextDouble();

答案 1 :(得分:0)

您可能希望将数据读取包装成if,如下所示:

if (tast.hasNextDouble())
   tast.next.Double;
else {
   // print something and exit
   System.out.println("Incorrect temperature value!!!);
   System.exit(1);
   }