我正在尝试编写一种计算复合兴趣的方法。这是我不断收到的错误消息:
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;
}
}
答案 0 :(得分:1)
您错过f
浮动格式。试试System.out.printf("%.2f\n",balance(1000.0, .05, 8.5));
答案 1 :(得分:0)
您忘了提及f
来指定那里的浮动格式。
System.out.printf("%.2f\n", balance(1000.0, .05, 8.5)); // .2f is the proper syntax
答案 2 :(得分:0)
这就是你写的
System.out.printf("%.2\n", balance(1000.0, .05, 8.5));
我认为你在写%。2f
时错过了fSystem.out.printf("%.2f\n", balance(1000.0, .05, 8.5));