2013-10-30 47 views
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; 
} 
} 
+0

精氨酸。我盯着那麼久。謝謝。 – user2918429

回答

1

您浮動格式錯過f。嘗試System.out.printf("%.2f\n",balance(1000.0, .05, 8.5));

0

你忘了提及f來指定那裏的浮點格式。

System.out.printf("%.2f\n", balance(1000.0, .05, 8.5)); // .2f is the proper syntax 
0

這是什麼ü寫了

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

我覺得你錯過了f當書面方式%.2f

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