0

私はJavaが初めてで、人の体温が低すぎたり高すぎたりしないかどうかを確認するプログラムを作成しようとしています。

次のエラー メッセージが表示されます:(int ではなく double を入力した場合)

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

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 に答える