java - 格式标志转换不匹配异常

标签 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)

最佳答案

你需要用另一个%来转义你的%

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

关于java - 格式标志转换不匹配异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52518632/

相关文章:

java - ARCore:如何检测光照不足?

java - 克服 super 上的重复方法静态引用

c - 为什么 C 程序使用 Scanf 给出奇怪的输出?

java - 为什么 org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping 不起作用?

java - Google Guice 属性管理

c - 有没有更好的方法来调整打印整数的缓冲区大小?

c - C函数打印错误

python - Eclipse 和 Python 3 : why does printf() from ctypes display in console output after subsequent print() statements

c - 将字典内容写入文件时出现段错误

Java 和 MySQL 控制台 REPL