具体代码
package org.example;
import java.math.BigDecimal;
//使用贴现率8%和12%分别计算每个项目的净现值(NPV)
public class Main {
public static void main(String[] args) {
int[] a1 = new int[]{-100000,10000,10000,10000,20000,100000};
int[] a2 = new int[]{-1000000,200000,200000,200000,200000,300000};
int[] a3 = new int[]{-100000,30000,30000,30000,30000,30000};
int[] a4 = new int[]{-120000,30000,30000,30000,30000,75000};
//贴现率
double m=0.08;
double n=0.12;
double[] t1=new double[6];
double[] t2=new double[6];
//计算每一年的每个项目的贴现因子
for(int j=0;j<=1;j++){
int k=0;
int l=0;
for(int i=0;i<=5;i++) {
if (j == 0) {
double q = 1.0 / Math.pow(1.0 + m, i);
BigDecimal bigDecimal = new BigDecimal(q);
q = bigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
t1[k++]=q;
System.out.println("第" + i + "年8%的贴现率的贴现因子是:" + q);
} else {
double q = 1.0 / Math.pow(1.0 + n, i);
BigDecimal bigDecimal = new BigDecimal(q);
q = bigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
t2[l++]=q;
System.out.println("第" + i + "年12%的贴现率的贴现因子是:" + q);
}
}
}
//计算每个项目的净现值
double result11=0;
double result21=0;
for(int j=0;j<6;j++){
result11=a1[j]*t1[j]+result11;
result21=a1[j]*t2[j]+result21;
}
System.out.println("项目1的8%的贴现率的净现值是:"+result11+"; 12%的贴现率的净现值是:"+result21);
double result12=0;
double result22=0;
for(int j=0;j<6;j++){
result12=a2[j]*t1[j]+result12;
result22=a2[j]*t2[j]+result22;
}
System.out.println("项目2的8%的贴现率的净现值是:"+result12+"; 12%的贴现率的净现值是:"+result22);
double result13=0;
double result23=0;
for(int j=0;j<6;j++){
result13=a3[j]*t1[j]+result13;
result23=a3[j]*t2[j]+result23;
}
System.out.println("项目3的8%的贴现率的净现值是:"+result13+"; 12%的贴现率的净现值是:"+result23);
double result14=0;
double result24=0;
for(int j=0;j<6;j++){
result14=a4[j]*t1[j]+result14;
result24=a4[j]*t2[j]+result24;
}
System.out.println("项目4的8%的贴现率的净现值是:"+result14+"; 12%的贴现率的净现值是:"+result24);
}
}