1

我正在尝试编写一种计算复利的方法。这是我不断收到的错误消息:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '.'
    at java.util.Formatter.checkText(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at Balance.main(Balance.java:5)

我不太确定从这里去哪里。如果有人能指出我正确的方向,将不胜感激。(System.out.printf("%.2\n", balance(1000.0, .05, 8.5));作业需要)

public class Balance {
public static void main(String[] args) {

    System.out.printf("%.2\n", balance(1000.0, .05, 8.5));
}

public static double balance(double initialBalance, double interestRate,
        double years) {

    double compoundedInterest = initialBalance * Math.pow(1 + interestRate,        years);
    return compoundedInterest;
}
}
4

3 回答 3

1

您错过f了浮动格式。尝试System.out.printf("%.2f\n",balance(1000.0, .05, 8.5));

于 2013-10-30T05:57:18.647 回答
0

你忘了提到在f那里指定浮点格式。

System.out.printf("%.2f\n", balance(1000.0, .05, 8.5)); // .2f is the proper syntax
于 2013-10-30T05:57:27.087 回答
0

这就是你写的

   System.out.printf("%.2\n", balance(1000.0, .05, 8.5));

我想你在写 %.2f 的时候错过了 f

System.out.printf("%.2f\n", balance(1000.0, .05, 8.5));
于 2013-10-30T09:29:50.017 回答