public class Interest {
public static void main(String[] args) {
int n; // number of years
double rate; // annual rate
for (int r = 5; r<=10; r++ ) {
System.out.println("\nInterest rate is " + r + "%");
System.out.println("**********************");
System.out.println("Year\tAmount on deposit");
for (n=1; n<=10; n++) {
int p = 1000; // original amount
rate = r / 100;
double a = p * (Math.pow(1 + rate, n)); // amount on deposit at the end of year
System.out.printf("\n%d\t%.2f", n, a);
}
}
}
}
它以年为单位正确显示了图表。但年末存款金额 (a) 保持不变 (1000)。看起来像这样;
Interest rate is 5%
**********************
Year Amount on deposit
1 1000.00
2 1000.00
3 1000.00
4 1000.00
5 1000.00
6 1000.00
7 1000.00
8 1000.00
9 1000.00
10 1000.00
它一直持续到利率达到 10%。
最佳答案
您正在执行 integer division .
尝试:rate = r/100.0;
关于java - 为什么程序不计算利息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059600/