软件过程与管理——计算净现值
题目:
计算公式:
实现代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.*; public class test { static Scanner in = new Scanner(System.in); public static void input(int a[], int n) {//数组输入 for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } } public static int[] year_npv(int c[], int d[], double r)// 计算年净现值并存入数组 { for (int t = 0; t < c.length; t++) { d[t] = (int) (c[t] / (Math.pow(1 + r, t))); } return d; } public static int sum_npv(int e[])// 计算最终净现值 { int sum = 0; for (int i = 0; i < e.length; i++) { sum += e[i]; } return sum; } public static void main(String[] args) { int a[] = new int[6];// 项目的年金额数组 int b[] = new int[6];// 年净现值数组 for (int i = 1; i <= 4; i++) { System.out.println("请输入项目" + i + "每年的金额:"); input(a, 6); int npv1 = sum_npv(year_npv(a, b, 0.08)); int npv2 = sum_npv(year_npv(a, b, 0.12)); System.out.println("项目的净现值为:贴现率8%:" + npv1 + " 贴现率12%:" + npv2); System.out.println(); } } }
结果:
可能因小数四舍五入问题导致最终结果略有偏差