InputMismatchException错误数据类型

时间:2015-03-25 18:11:49

标签: java

我第一次玩Java,出现错误。我不明白为什么。

错误是:

  

java.util.InputMismatchException

它说问题在第20行("price = in.nextDouble();"),但我无法理解这个问题。

看起来问题发生在我插入天数,每天的费用和我使用小数后,我举了例如34.98。使用double是不是能够使用十进制数?

以下是代码:

int days;
        double price;

        System.out.println("How many days you will be here?");

        Scanner in = new Scanner(System.in);

        days = in.nextInt();

       System.out.println("How much you can afoord per day?");
       price = in.nextDouble();

        double total = price * days;
        System.out.println("Youll be paying "+ total);

2 个答案:

答案 0 :(得分:0)

此代码适用于我

import java.util.Scanner;

public class HelloWorld {
  public static void main(String[] args) {
    int days;
    double price;

    System.out.println("How many days you will be here?");

    Scanner in = new Scanner(System.in);

    days = in.nextInt();

    System.out.println("How much you can afoord per day?");
    price = in.nextDouble();

    double total = price * days;
    System.out.println("Youll be paying "+ total);

  }
}

输出

Executing Build Command: javac /root/HelloWorld.java                                                                                                                                                                                                                                                             
How many days you will be here?                                                                                                                                                               
3                                                                                                                                                                                             
How much you can afoord per day?                                                                                                                                                              
34.98                                                                                                                                                                                          
Youll be paying 104.94                                                                                                                                                                          
Process exited successfully

答案 1 :(得分:0)

所以我发现了问题,我必须在Scanner之后添加..." in.useLocale(Locale.US);" 这就是它出错的原因。