使用贴现率8%和12%分别计算每个项目的净现值(NPV),提交源代码和实验结果截图,编程语言不限。
我这里用java实现的
代码如下:
package test; import java.math.BigDecimal;
//作者:刘子煜 public class test1 { public static void main(String[] args) { int[] project1=new int[]{-100000,10000,10000,10000,20000,100000}; int[] project2=new int[]{-1000000,200000,200000,200000,200000,300000}; int[] project3=new int[]{-100000,30000,30000,30000,30000,30000}; int[] project4=new int[]{-120000,30000,30000,30000,30000,75000}; double[] tiexian=new double[]{0.08,0.12}; double[][] b=new double[2][6]; //int project1result1=jia(project1,tiexian); for(int j=0;j<2;j++){ for(int i=0;i<6;i++){ b[j][i]=1/Math.pow((tiexian[j]+1), i); BigDecimal t = new BigDecimal(b[j][i]); b[j][i]=t.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(); } } for(int j=0;j<2;j++){ for(int i=0;i<6;i++){ if(j==0){ System.out.println("第"+i+"年"+"贴现率8%的贴现因子"+b[j][i]); }else{ System.out.println("第"+i+"年"+"贴现率12%的贴现因子"+b[j][i]); } } } int[] project1result1=doit(project1,b);System.out.println("项目1中贴现率8%的净现值:"+project1result1[0]+" 贴现率12%的净现值:"+project1result1[1]); int[] project1result2=doit(project2,b);System.out.println("项目2中贴现率8%的净现值:"+project1result2[0]+" 贴现率12%的净现值:"+project1result2[1]); int[] project1result3=doit(project3,b);System.out.println("项目3中贴现率8%的净现值:"+project1result3[0]+" 贴现率12%的净现值:"+project1result3[1]); int[] project1result4=doit(project4,b);System.out.println("项目4中贴现率8%的净现值:"+project1result4[0]+" 贴现率12%的净现值:"+project1result4[1]); } static int[] doit(int[] a, double[][] b){ int[] p=new int[2]; for(int j=0;j<2;j++){ int temp=0; for(int i=0;i<6;i++){ temp=(int)(temp+a[i]*b[j][i]); System.out.println(temp+"-----"); } p[j]=temp; } //System.out.println("8%的净现值:"+p[0]+" 12%的净现值:"+p[1]); return p; } }