日期:2014-05-20  浏览次数:20926 次

求大神帮忙编一个初级java(内附题目)
Write a Java program for a bank that will calculate the monthly interest earned on a cashdeposit. The user enters the amount deposited, the term, and interest rate. The program willthen display a table listing for the month, interest, total interest, and account balance for eachperiod. For example, if the amount deposited is $2000.00, the term is 6 months and theinterest rate is 2%, the following output will be displayed: 
编写一个Java程序,将计算银行每月的现金存款所赚取的利息。用户输入存入的金额,期限,利率。该计划将显示表中列出的为一个月,利息,利息总额,帐户余额为每个时期。例如,如果存入的金额是$ 2000.00,期限为6个月,利率为2%,将显示以下输出:

Period Interest Total Interest Total Balance
1 6.66 6.66 2006.66
2 6.69 13.35 2013.35
3 6.71 20.06 2020.06
4 6.74 26.80 2026.80
5 6.75 33.55 2033.55
6 6.78 40.33 2040.33

Initial deposit: $2000.00
Total Interest: $40.33
Total Balance: $2040.33

------解决方案--------------------
Java code

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
Java code
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);
    }
}