1

我很早就问了一个问题并得到了一些很大的帮助:D我的第一个问题。我以为我走出了困境,但我想不是。我正在设置/形成我的程序正在获取的信息,但是在尝试打印我的变量“时间”时遇到了问题,如果有人能指出我做错了什么,我将非常感激。

我认为是问题的代码部分是:

// this class prints a loan statement showing the amount borrowed
    // and the amount borrowed, the annual interest rate, the number of months
    // and the monthly payment
    public static void loanStatement(double principle, double interest, int time, double mPayment)
    {
       System.out.printf("%2s%13s%8s%20s%n", "Principle", "Interest", "Time", "Monthly Payment");
       System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest +time);
       System.out.println("monthly payment is" +mPayment);
       System.out.println("interest is" +interest);
       System.out.println("Time is" +time);

当我取出 +time 并删除 10.2f(我尝试了许多不同的组合)时,我没有收到任何错误。我还打印了时间和每月付款变量,并得到了一个奇怪的数字网格。

我放入了其他 printlns,因为我想检查程序是否返回了正确的值,并且确实如此。

下面是我得到的格式错误

Principle     Interest    Time     Monthly Payment
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '10.2f'
15000.00     36.07  at java.util.Formatter.format(Formatter.java:2487)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at loanpayment.LoanPayment.loanStatement(LoanPayment.java:89)
    at loanpayment.LoanPayment.main(LoanPayment.java:55)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)

我希望我的要求对你们有意义,并提前感谢您的帮助!

4

2 回答 2

0

您在此处仅提供两个输入:

   System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest +time);

而它需要三个。也许你错过了把逗号(,)放在前面+time 如果你合乎逻辑试试这个:

   System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest, +time);
于 2013-10-07T02:43:57.900 回答
0

您的格式中缺少逗号...

System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest +time);
                                                              ^--- Missing comma

哪个试图添加最后两个值,请尝试

System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest, +time);

反而...

于 2013-10-07T02:44:23.557 回答