標籤:
計算:1.本金為100萬,利率或者投資報酬率為3%,投資年限為30年,那麼,30年後所獲得的利息收入:按複利計算公式來計算就是:1,000,000×(1+3%)^30
客戶提出:
2.如果按照單利計算,本息又是多少呢?
3.假如30年之後要籌措到300萬元的養老金,平均的年回報率是3%,那麼,現在必須投入的本金是多少呢?
4.利率這麼低,複利計算收益都這麼厲害了,如果拿100萬元去買年報酬率10%的股票,若一切順利,過多長時間,100萬元就變成200萬元呢?
5.如果我希望在十年內將100萬元變成200萬元,應該找到報酬率在多少的投資工具來協助我達成目標?如果想在5年後本金翻倍,報酬率就應至少為多少才行呢?
6.如果每年都將積蓄的3萬元進行投資,每年都能獲得3%的回報,然後將這些本利之和連同年金再投入新一輪的投資,那麼,30年後資產總值將變為多少?如果換成每月定投3000呢?
7. 你寫的程式能讓客戶隨意操作嗎?誤輸入資料、不小心做了非常規的操作程式是什麼反應?
相互觀賞程式運行情況。
8. 如果向銀行貸款10萬元,年利率6.5%,期限為10年,那麼每月等額本息還款多少?(算複利條件下等額還款金額)
package FuLi;
import java.util.*;
import java.math.*;
import java.text.*;
import org.omg.CORBA.PUBLIC_MEMBER;
public class gzh {
public int NianXian(double P,double F,double i){
int N=(int) ((Math.log(F) / Math.log(1 + i)) - (Math.log(P) / Math
.log(1 + i)));
return N;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎使用複利計算機!--BY 郭志豪");
/*
* 複利計算公式: F=P*(1+i)N(次方) F:複利終值 P:本金 i:利率 N:利率擷取時間的整數倍
*/
gzh bb = new gzh();
Scanner input = new Scanner(System.in);
System.out.println("複利計算機:");
System.out.println("本金按1 複利(單利)按2 年限按3 利率按4 年金終值按5 定期付息按6");
int num = input.nextInt();
DecimalFormat df1 = new DecimalFormat("#.00");
// 求本金
if (num == 1) {
System.out.println("請輸入複利終值:");
double F = input.nextDouble();
System.out.println("請輸入年利率:");
double i = input.nextDouble();
System.out.println("請輸入存入年限:");
int N = input.nextInt();
double P = F / Math.pow(1 + i, N); // 本金
System.out.println("所需本金為:" + df1.format(P));
}
// 求複利(單利)
else if (num == 2) {
System.out.println("請輸入存入本金:");
double P = input.nextDouble();
System.out.println("請輸入年利率:");
double i = input.nextDouble();
System.out.println("請輸入存入年限:");
int N = input.nextInt();
double D = P * (1 + i * N); // 單利
System.out.println("單利終值:" + df1.format(D));
System.out.println("請輸入年複利次數:");
int m = input.nextInt();
double F = P * (Math.pow((1 + i / m), N * m)); // 複利
System.out.println("複利終值為:" + df1.format(F));
}
// 求所需年限
else if (num == 3) {
System.out.println("請輸入存入本金:");
double P = input.nextDouble();
System.out.println("請輸入複利終值:");
double F = input.nextDouble();
System.out.println("請輸入年利率:");
double i = input.nextDouble();
int N1 = (int)bb.NianXian(F, i, P);
//int N = (int) ((Math.log(F) / Math.log(1 + i)) - (Math.log(P) / Math
//.log(1 + i)));
System.out.println("所需年限為:" +N1);
} else if (num == 4) {
System.out.println("請輸入存入本金:");
double P = input.nextDouble();
System.out.println("請輸入複利終值:");
double F = input.nextDouble();
System.out.println("請輸入存入年限:");
int N = input.nextInt();
double i = (Math.pow(F / P, 1.0 / N)) - 1;
System.out.println("年利率為:" + i);
System.out.println("本金n年後是否會翻n番?是請按1");
int up = input.nextInt();
if (up == 1) {
System.out.println("請輸入幾年後:");
int n = input.nextInt();
System.out.println("翻幾番:");
int f = input.nextInt();
double I = 72.0 / (n / f); // 72法則
System.out.println(n + "年後本金翻倍的年利率至少為:" + df1.format(I));
}
if (up != 1) {
System.out.println("歡迎再來!");
}
} else if (num == 5) {
System.out.println("按年投資按1 按月投資按2 ");
int number = input.nextInt();
if (number == 1) {
System.out.println("要投資的本金:");
double P = input.nextDouble();
System.out.println("獲得的年利率回報:");
double i = input.nextDouble();
System.out.println("投資年限(年):");
double N = input.nextInt();
double F = P * (Math.pow(1 + i, N) - 1) / i;
System.out.println("按年投資的年金終值為:" + df1.format(F));
}
if (number == 2) {
System.out.println("要投資的本金:");
double P = input.nextDouble();
System.out.println("獲得的年利率回報:");
double i = input.nextDouble();
System.out.println("投資年限(年):");
double N = input.nextInt();
double F = P * (Math.pow(1 + i / 12.0, N * 12) - 1) / (i / 12);
System.out.println("按月投資的年金終值為:" + df1.format(F));
}
if (number != 1 || number != 2) {
System.out.println("輸入錯誤!");
}
} else if (num == 6) {
System.out.println("要貸款的本金:");
double P = input.nextDouble();
System.out.println("貸款年利率:(年)");
double i = input.nextDouble();
System.out.println("貸款期(年):");
double N = input.nextDouble();
double H = P
* (((i / 12) * (Math.pow(1 + i / 12, N * 12))) / (Math.pow(
1 + i / 12, N * 12) - 1));
System.out.println("需每月還款:" + df1.format(H));
} else {
System.out.println("輸入錯誤!");
}
}
}
Junit測試
代碼還沒最佳化完整,接下來會繼續完成,其他測試也跟這個一樣,先列舉一個!
總結:從一開始到現在,不斷出現問題,不斷地解決問題,作品慢慢成型,越來越有幹勁,這就是編程的魅力!
複利計算機(軟體工程)及Junit測試———郭志豪