gpt4 book ai didi

java - 打印形成

转载 作者:行者123 更新时间:2023-12-01 04:18:25 25 4
gpt4 key购买 nike

我很早就提出了一个问题并得到了一些很大的帮助: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);

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

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

下面是我遇到的格式错误

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)

我希望我的问题对你们来说有意义,并提前感谢您的帮助!

最佳答案

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

   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);

关于java - 打印形成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19216330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com