java收据

java收据,java,csc,receipt,Java,Csc,Receipt,我需要做一张收据,格式要尽量像普通收据一样。姓名、地址、时间和日期都在顶部。(所有这些都需要用户输入。) 主代码 //Removed Imports class ReceiptCode { public static void main(String[] args) { // TODO Auto-generated method stub //Font f = new Font("Calibri", Font.BOLD, 20);

我需要做一张收据,格式要尽量像普通收据一样。姓名、地址、时间和日期都在顶部。(所有这些都需要用户输入。)

主代码

    //Removed Imports

class ReceiptCode {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Font f = new Font("Calibri", Font.BOLD, 20);

        Scanner scan= new Scanner(System.in);
        System.out.println("Enter Company Name");
        String companyName= scan.nextLine();

        System.out.println("Enter STREET ADDRESS");
        String street=scan.nextLine();

        System.out.println("Enter CITY, STATE, ZIP");
        String CSZ=scan.nextLine();


        String breaker = "------------------------------";
        List <Items> invList = new ArrayList<Items>();
        System.out.println("How many items did you order?");
        int counter = scan.nextInt();
        double totalPrice = 0;
        for (int i=0; i<counter; i++)
        {
            System.out.println("Name the item");
            String fName = scan.next();
            System.out.println("How many of this item did you order?");
            int fType = scan.nextInt();
            System.out.println("What was the price?");
            double fPrice = scan.nextDouble();
            Items inv = new Items(fName, fType, fPrice);
            double x = (fType * fPrice);
            totalPrice += x;
            invList.add(inv);
            System.out.println(totalPrice);
        }

        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        DateFormat timeFormat = new SimpleDateFormat ("HH:mm");
        Date date = new Date();
        Date time = new Date();
        System.out.printf("%-15s %n", companyName);
        System.out.printf("%-15s %14s %n",street + "\n" + CSZ,dateFormat.format(date));
        System.out.printf("%-15s %14s %n", timeFormat.format(time));
        System.out.println(breaker);
        for (Items c : invList) {
               System.out.println (c.getFoodAmmount() + " x " + c.getFoodName() + " : " + c.getFoodPrice() + "$");
               System.out.println (breaker);

}   
}
}       

编译代码时,我收到了一个异常:线程“main”java.util.MissingFormatArgumentException中的异常:格式说明符“14s”。如何解决此问题?

您收到异常,因为您没有在时间线上设置%14s(无法在printf语句中找到变量)

此行包含错误。如果将其更改为:

System.out.printf("%-15s %n", timeFormat.format(time));

它应该能正常工作。

所以。。。你的问题是什么?
System.out.printf("%-15s %14s %n", timeFormat.format(time));
System.out.printf("%-15s %n", timeFormat.format(time));