第一个月之后,账户上的值就变成:
100 * (1 + 0.00417) = 100.417
第二个月之后,账户上的值就变成:
(100 + 100.417) * (1 + 0.00417) = 201.252
第三个月之后,账户上的值就变成:
(100 + 201.252) * (1 + 0.00417) = 302.507
以此类推。
编写程序显示六个月后账户上的钱数。(在编程练习题5.30中,你将使用循环来简化这里的代码,并能显示任何一个月之后的账户值。)
Enter the monthly saving amount: 100
After the sixth month,the account value is $608.81
package Second;
import java.util.Scanner;
public class Code13 {
public static void main(String[] args) {
System.out.print("Enter the monthly saving amount:");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
final double month = 0.00417;
double sum = 0;
for(int i = 1;i <= n; i++) {
sum =(100 + sum) * (1 + 0.00417);
}
System.out.println("After the sixth month,the account value is $" + sum);
}
}
输出
Enter the monthly saving amount:6 After the sixth month,the account value is $608.8181155768638