日期:2014-05-20 浏览次数:20988 次
import java.util.Scanner; public class Test12 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("please input amount deposited"); double amount = sc.nextDouble(); System.out.println("please input term"); int term = sc.nextInt(); System.out.println("please input interest rate"); double rate = sc.nextDouble(); rate = Math.pow((1+rate/100),1.0/6); System.out.println(rate); printMsg(amount,term,rate); } public static void printMsg(double amount,int term,double rate){ double total_interest = 0; double current_interest; for(int i=0;i<term;i++){ current_interest = amount*(rate-1); total_interest += current_interest; System.out.println(i+1+" "+current_interest+" "+total_interest+" "+(amount+current_interest)); amount *= rate; } } }
------解决方案--------------------
for example
import java.util.*; //import java.math.*; public class Test { public static void main(String[] args) throws Throwable { /* Scanner sc = new Scanner(System.in); System.out.println("please input balance"); double balance = Double.parseDouble(sc.nextLine()); System.out.println("please input period"); int period = Integer.parseInt(sc.nextLine()); System.out.println("please input rate"); double rate = Double.parseDouble(sc.nextLine()); calc(balance, rate, period); */ calc(balance, rate, period); } public static void calc(double balance, double rate, int period) { System.out.println("Period Interest Total Interest Total Balance"); /* BigDecimal b = new BigDecimal(balance); BigDecimal t = new BigDecimal(balance); BigDecimal r = new BigDecimal(rate/period); for (int i=0; i<period; i++) { t = t.multiply(r); t = t.setScale(2, BigDecimal.ROUND_UP); b = b.add(t); System.out.printf("%s, %s, %s\n", t.toString(), b.subtract(new BigDecimal(balance)).setScale(2, BigDecimal.ROUND_UP).toString(), b.setScale(2, BigDecimal.ROUND_UP).toString() ); t = b; } */ double b = balance; double t = balance; double r = rate/period; for (int i=0; i<period; i++) { t *= r; b += t; System.out.printf("%.2f, %.2f, %.2f\n", t, b-balance, b ); t = b; } System.out.println(); System.out.printf("Initial deposit: $%.2f\n", balance); System.out.printf("Total Interest: $%.2f\n", b-balance); System.out.printf("Total Balance: $%.2f", b); } }