【问题标题】:Formatting Numeric Print Output Exception格式化数字打印输出异常
【发布时间】:2016-10-21 09:00:47
【问题描述】:
Exception in thread "main" java.util.InputMismatchException  
    at java.util.Scanner.throwFor(Scanner.java:864)  
at java.util.Scanner.next(Scanner.java:1485)    
at java.util.Scanner.nextInt(Scanner.java:2117)     
at java.util.Scanner.nextInt(Scanner.java:2076)     
at Commission2.main(Commission2.java:38)

一开始它运行良好,直到一切都崩溃了。现在我需要帮助。有人吗?

import java.util.Scanner;//程序需要scanner 导入 java.text.DecimalFormat;

public class Commission2
{

public static void main(String args[])

    {

        //create Scanner
    Scanner input = new Scanner(System.in);

    //format decimal with two digits
    DecimalFormat twoDigits = new DecimalFormat("0.00");
    //format decimal with three digits
    DecimalFormat threeDigits = new DecimalFormat("0.000");

    //declare all variables
    int size,count = 0;
    int pay = 200;
    double commission = 9/100;
    double result = 0;
    double item, itemtotal = 0;

    //get the limit of the data entry (data validation technique)
    do{
        System.out.printf("Enter the number of items :");
            size = input.nextInt();

    }while(size < 0);

    //data entry
    while (count < size) {

        System.out.print("Enter price of item #" +(count + 1) +": ");
            item = input.nextInt();


        /*Processing!*/
        itemtotal += item;
        ++count;


    }//end while
        result = (itemtotal * commission) + pay;

    System.out.printf("%s%d\n","The total earnings for this week is $",result);




    }
}

【问题讨论】:

  • Commission2.main(Commission2.java:38),检查第 38 行,很可能您输入的数据类型错误
  • @Ankit Deshpande 我不认为你是正确的。我在发布之前检查了这一点。我发布这个的原因是因为我被难住了。
  • @Gerald Loo 你能添加失败的输入吗?
  • @Andrea 如果我很粗鲁,我很抱歉。是您的评论/建议只是在代码中添加了更多错误消息。
  • 您确定要提供integers 供您输入吗?我试过你的代码,它适用于整数输入,但它在最后一行System.out.printf("%s%d\n","The total earnings for this week is $",result); 崩溃。您需要将其更改为 System.out.printf("%s%f\n", "The total earnings for this week is $", result);,因为 Lucian Nut 正确回答。

标签: java printing formatting output numeric


【解决方案1】:

在第 25 行,您声明了 double item,但在第 38 行,您尝试将 input.nextInt() 分配给变量 item。

如果您要在此处提供 Integer 值 (5),则代码应按预期工作。但是,当提供 Double (5,12) 时,将抛出 InputMismatchException 异常。

要解决此问题,只需更改以下内容:

item = input.nextInt();

到这里:

item = input.nextDouble();

【讨论】:

    【解决方案2】:
    System.out.printf("%s%f\n", "The total earnings for this week is $", result);
    

    因为 %d 只适用于整数,而 %f 适用于浮点数和双精度数,所以会起作用。

    【讨论】:

    • 是的,这是唯一的代码崩溃,前提是用户输入的值是整数!
    【解决方案3】:

    因为它在 item = input.nextInt(); 上失败了 当您输入值时,看起来好像您尝试使用的不是 int 值。请检查一下。 你为什么不发布你使用的输入?

    【讨论】:

      【解决方案4】:

      在您的 printf 中,您使用十进制整数格式来打印浮点数/双精度数。 而不是 %d 使用 %f。

      Please have a look at this link for more information

      【讨论】:

        猜你喜欢
        • 2017-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多