为什么我得到例外? [英] Why am I getting an exception?

查看:221
本文介绍了为什么我得到例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙。从扫描仪的提示显示到控制台后,我收到此错误:

Please help. I get this error after the prompts from the Scanner are displayed to the console:

Exception in thread "main" java.util.IllegalFormatPrecisionException: 2
at java.util.Formatter$FormatSpecifier.checkInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Project6.main(Project6.java:56)

问题来自printf吗?我检查了格式,它看起来正确。

这是程序:

Is the problem coming from the printf? I've checked the formatting, and it looks right.
Here is the program:

import javax.swing.JOptionPane; // allows for message dialog

import java.util.Scanner; // allows for Scanner object to be used

import java.text.DecimalFormat; // allows for formatting numbers


public class Project6 { 


public static void main (String[] args)  {      

    final double LUXURYRATE = .2; // rate of luxury tax
    final double STATERATE = .1; // rate of state tax
    final double LABORRATE = .05; // rate of labor cost

    double diamondCost; // cost of diamond, value comes from the Scanner
    double settingCost; // cost of setting, value comes from the Scanner
    int numOrdered; // quantity ordered, value comes from the Scanner
    double baseCost ; //sum of the values in the variables for diamond and  setting
    double totalCost; // total cost including tax
    double laborCost; // cost of labor
    double stateTax; // cost of state tax
    double luxuryTax; // cost of luxury tax
    double finalAmountDue; // total cost times quantity ordered

    DecimalFormat formatter = new DecimalFormat("$#,##0.00"); // money format

        Scanner input = new Scanner (System.in); // creates Scanner object  
        System.out.print("Enter diamond cost:"); // prompts user
        diamondCost = input.nextDouble(); // converts input to double

        System.out.print("Enter setting cost:"); // prompts user
        settingCost = input.nextDouble(); // converts input to double

        System.out.print("Enter quantity:"); // prompts user
        numOrdered = input.nextInt(); // converts input to integer          

        baseCost = diamondCost + settingCost; // calculates the cost before multiplication

        laborCost = calcExtraCost(LABORRATE , baseCost); // calls method calcExtraCost for calculation
        stateTax = calcExtraCost(STATERATE, baseCost); // calls method calcExtraCost for calculation
        luxuryTax = calcExtraCost(LUXURYRATE, baseCost); // calls method calcExtraCost for calculation

        totalCost = baseCost + laborCost + stateTax + luxuryTax; // total cost including tax and labor costs
        finalAmountDue = totalCost * numOrdered; // calculates total    

    System.out.printf("Diamond Cost: $%,.2d \n", diamondCost); // displays bill to console
    System.out.printf("Setting Cost: $%,.2f \n", settingCost);
    System.out.printf("State Tax @ 10%: $%,.2f \n", stateTax);
    System.out.printf("Luxury Tax @ 20%: $%,.2f \n", luxuryTax);
    System.out.printf("Labor Charge @ $5% %,.2f \n", laborCost);
    System.out.printf("Total Price: $%,.2f \n" , totalCost);
    System.out.printf("Number Ordered: $%,.2d \n", numOrdered);
    System.out.printf("Amount Due: $%,.2f \n", finalAmountDue);


    JOptionPane.showMessageDialog(null, "Diamond Cost: " + formatter.format(diamondCost) + "\n" + 
                                        "Setting Cost: " + formatter.format(settingCost) + "\n" +
                                        "State Tax @ 10%: " + formatter.format(stateTax) + "\n" +
                                        "Luxury Tax @ 20%: " + formatter.format(luxuryTax) + "\n" + 
                                        "Labor Charge @ 5% " + formatter.format(laborCost) + "\n" +
                                        "Total Price: " + formatter.format(totalCost) + "\n" +
                                        "Number Ordered: " + formatter.format(numOrdered) + "\n" +
                                        "Amount Due: " + formatter.format(finalAmountDue) ); // displays bill in message dialog     


} // end method main 


public static double calcExtraCost (double RATE, double bcost) {

    double cost = RATE * bcost; // calculates extra cost

    return cost; // returns value of extra cost



     } // end method calcExtraCost


} // end class Project6


推荐答案

A:因为格式说明符与输入参数不匹配 printf 方法。

A: Because your format specifiers don't match input arguments used in the printf method.

使用%f 而不是%d 作为 double 值的格式说明符字符

Use %f rather than %d as the format specifier character for double values

System.out.printf("Diamond Cost: $%,.2f \n", diamondCost); 
                                      ^

另外需要额外的来转义该角色

In addition % requires an additional % to escape that character

System.out.printf("State Tax @ 10%%: $%,.2f \n", stateTax);

最后删除不必要的点字符

Finally, remove the unnecessary dot character

System.out.printf("Number Ordered: $%,.2d \n", numOrdered);
                                      ^

阅读:格式化程序javadoc

这篇关于为什么我得到例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆