今天完成了王輝老師要求的淨現值計算 以下為程式碼
package tast;
import java.math.BigDecimal;
public class Tast {
public static void main(String[] args) {
// TODO 自動生成的方法存根
// 建立二維陣列儲存每個專案每年利潤
int [][] money = new int [5][4];
// 填入利潤
money[0][0] = -100000; money[0][1] = -1000000; money[0][2] = -100000; money[0][3] = -120000;
money[1][0] = 10000; money[1][1] = 200000; money[1][2] = 30000; money[1][3] = 30000;
money[2][0] = 10000; money[2][1] = 200000; money[2][2] = 30000; money[2][3] = 30000;
money[3][0] = 10000; money[3][1] = 200000; money[3][2] = 30000; money[3][3] = 30000;
money[4][0] = 20000; money[4][1] = 200000; money[4][2] = 30000; money[4][3] = 30000;
// 貼現率陣列 貼現因子
double[] txl = new double[] {0.08,0.12};
double [] [] tx = new double [2][6];
// 貼現因子計算
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 6; j++) {
// 計算貼現因子
tx[i][j] = 1/Math.pow(1+txl[i], j);
// 將因子四捨五入
BigDecimal b = new BigDecimal(tx[i][j]);
tx[i][j] = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
// 輸出
if(i==0)
System.out.println("貼現率為0.08 , 第"+j+"年的貼現因子為 : "+tx[i][j]);
else
System.out.println("貼現率為0.12 , 第"+j+"年的貼現因子為 : "+tx[i][j]);
}
}
// 淨現值計算
for (int i = 0; i < 2; i++) {
for (int j2 = 0; j2 < 4; j2++) {
double jx = 0 ;
for (int j = 0; j < 6; j++) {
// 計算每年淨現值
jx = money[j][j2] * tx[i][j]+jx;
}
// 輸出
if(i==0)
System.out.println("貼現率為0.08 , 專案"+(j2+1)+" 的淨現值為 : "+jx);
else
System.out.println("貼現率為0.12 , 專案"+(j2+1)+" 的淨現值為 : "+jx);
}
}
}
}