我提前问了一个问题,得到了一些很好的帮助:D My first question。我以为我已经走出了困境,但我猜不是。我正在设置/形成我的程序正在获取的信息,但是当我尝试打印变量“时间”时遇到问题,如果任何人可以指出我做错了什么我会非常感激。
我认为问题的代码部分是:
// 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);
当我取出+时间并删除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)
我希望我所要求的对你们有意义,并且感谢您提前提供任何帮助!
答案 0 :(得分: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);
答案 1 :(得分: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);
,而不是...