0

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

我一直收到此错误消息:(当我输入双精度而不是整数时)

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");
    }
}   
   } 
4

2 回答 2

3

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

tast.nextDouble();
于 2013-10-10T17:06:39.333 回答
0

您可能希望将读取的数据包装成if这样的:

if (tast.hasNextDouble())
   tast.next.Double;
else {
   // print something and exit
   System.out.println("Incorrect temperature value!!!);
   System.exit(1);
   }
于 2013-10-10T17:30:13.457 回答