首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSC java收据

CSC java收据
EN

Stack Overflow用户
提问于 2013-11-06 19:57:29
回答 1查看 1.3K关注 0票数 0

我需要做一个收据格式最好,我可以喜欢正常的收据。上面写着姓名和地址,时间和日期。(所有这些都需要用户输入。)

主代码

代码语言:javascript
复制
    //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);

}   
}
}       

这是Items数组列表的类

ArrayList

代码语言:javascript
复制
package Receipt;

public class Items {

        private String foodName;
        private int foodAmmount;
        private double foodPrice;

    public Items (String fdType, int fdAmmount, double fdPrice)
    {
        foodName = fdType;
        foodAmmount = fdAmmount;
        foodPrice = fdPrice;
    }
    public String getFoodName()
    {
        return foodName;
    }
    public int getFoodAmmount()
    {
        return foodAmmount;
    }
    public double getFoodPrice()
    {
        return foodPrice;
    }
}

当我编译代码时,我会收到一个关于:Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '14s'的异常。我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-06 21:07:11

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

代码语言:javascript
复制
System.out.printf("%-15s %14s %n", timeFormat.format(time));

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

代码语言:javascript
复制
System.out.printf("%-15s %n", timeFormat.format(time));

它应该能正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19821553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档