System.printf("%.2f", currentBalance) 工作正常,但问题是句子后面出现了四舍五入的数字。把代码放到你的eclipse程序中运行,你会发现肯定有问题。如果有人可以提供帮助,将不胜感激。
public class BankCompound {
public static void main (String[] args) {
compound (0.5, 1500, 1);
}
public static double compound (double interestRate, double currentBalance, int year) {
for (; year <= 9 ; year ++) {
System.out.println ("At year " + year + ", your total amount of money is ");
System.out.printf("%.2f", currentBalance);
currentBalance = currentBalance + (currentBalance * interestRate);
}
System.out.println ("Your final balance after 10 years is " + currentBalance);
return currentBalance;
}
}