格式标志转换不匹配异常

时间:2018-09-26 13:13:16

标签: java printf

编写一个程序,输入三个项目的名称,数量和价格。该名称可能包含空格。输出税率为6.25%的票据。所有价格应输出到小数点后两位。帐单的格式应为:名称分别为30个字符,数量为10个字符,价格为10个字符,总计为10个字符。示例输入和输出如下所示:

import java.util.Scanner;

public class ProjectLab {

    public static final double SALES_TAX = 8.625;

    public static void main(String[]args) {

        Scanner input = new Scanner(System.in);

        String item1, item2, item3;

        int quantity1, quantity2, quantity3;

        double price1, price2, price3;

        //Input for the first Item
        System.out.println("Input the name of item 1: ");
        item1 = input.nextLine();

        System.out.println("Input quantity of item 1: ");
        quantity1 = input.nextInt();

        System.out.println("Input price of item 1: ");
        price1 = input.nextDouble();

        String junk = input.nextLine(); //Junk Line
        //Input for the second Item
        System.out.println("Input name of item 2: ");
        item2 = input.nextLine();

        System.out.println("Input quantity of item 2: ");
        quantity2 = input.nextInt();

        System.out.println("Input price of item 2: ");
        price2 = input.nextDouble();

        String junk2 = input.nextLine(); //Junk line 2
        //Input for the third item
        System.out.println("Input name of item 3: ");
        item3 = input.nextLine();

        System.out.println("Input quantity of item 3: ");
        quantity3 = input.nextInt();

        System.out.println("Input price of item 3: ");
        price3 = input.nextDouble();

        double subtotal1 = price1 * quantity1;
        double subtotal2 = price2 * quantity2;
        double subtotal3 = price3 * quantity3;

        System.out.println("Your bill: ");
        System.out.println("Item      Quantity    Price    Total");
        System.out.println(item1 + "    " + quantity1 + "   " + price1 + "  " + subtotal1 );
        System.out.println(item2 + "    " + quantity2 + "   " + price2 + "  " + subtotal2 );
        System.out.println(item3 + "    " + quantity3 + "   " + price3 + "  " + subtotal3 );

        double finalSubtotal = (subtotal1 + subtotal2 + subtotal3);

        System.out.printf("Subtotal %.2f          \n" , finalSubtotal);

        double tax = (finalSubtotal / SALES_TAX); 

        System.out.printf("8.265% Sales tax %.2f\n        ", tax);

        double total = tax + finalSubtotal;

        System.out.printf("Total %.2f                     ", total);
    }
}

输出:

Input the name of item 1: 
Gummi Bears
Input quantity of item 1: 
10
Input price of item 1: 
1.29
Input name of item 2: 
Monster Energy
Input quantity of item 2: 
3
Input price of item 2: 
2.97
Input name of item 3: 
Ruffles Chips
Input quantity of item 3: 
20
Input price of item 3: 
1.49
Your bill: 
Item      Quantity    Price    Total
Gummi Bears 10  1.29    12.9
Monster Energy  3   2.97    8.91
Ruffles Chips   20  1.49    29.8
Subtotal    51.61         
Exception in thread "main" java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags =  
    at java.util.Formatter$FormatSpecifier.failMismatch(Formatter.java:4298)
    at java.util.Formatter$FormatSpecifier.checkBadFlags(Formatter.java:2997)
    at java.util.Formatter$FormatSpecifier.checkGeneral(Formatter.java:2955)
    at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2725)
    at java.util.Formatter.parse(Formatter.java:2560)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at ProjectLab.main(ProjectLab.java:65)

2 个答案:

答案 0 :(得分:1)

您需要用另一个%逃脱%

System.out.printf("8.265%% Sales tax %.2f\n        ", tax);

答案 1 :(得分:0)

在最后3个输出中使用String.format(..)

尝试一下

// also "\n" can be replaced with println
System.out.println(String.format("Subtotal %.2f          " , finalSubtotal));
// also escape the first % sign
System.out.println(String.format("8.265 %% Sales tax %.2f        ", tax));

More information for string fromatting here