JIANGzihao0222

导航

 

折现率公式:

 CF0:成本

CFi:第i期收入金额

r:折现率

java代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Test {

    public static double calculateNPV(double initialInvestment, ArrayList<Double> cashFlows, double discountRate) {
        double npv = -initialInvestment; // 初始投资为负值

        for (int t = 0; t < cashFlows.size(); t++) {
            double cashFlow = cashFlows.get(t);
            npv += cashFlow / Math.pow(1 + discountRate, t + 1); // 计算每期的现金流折现值并累加
        }

        return npv;
    }

    public static void main(String[] args) {

        double initialInvestment = 0; // 初始投资金额
        System.out.println("请输入初始投资金额/(元):");

        Scanner sc = new Scanner(System.in);
        initialInvestment = sc.nextDouble();

        System.out.println("请输入贴现率/(%):");
        double discountRate = sc.nextDouble() * 0.01; // 贴现率为10%

        System.out.println("请输入几期:");
        int num = sc.nextInt(); // 几期完成

        System.out.println("请输入每期现金数量/(元):");
        ArrayList<Double> cashFlows = new ArrayList<Double>(); // 未来现金流量列表
        for (int t = 0; t < num;t ++)
        {
            double temp = sc.nextDouble();
            cashFlows.add(temp);
        }

        // 计算净现值
        double npv = calculateNPV(initialInvestment, cashFlows, discountRate);
        System.out.println("净现值为: " + npv);
    }

}

 

posted on 2024-03-11 21:39  实名吓我一跳  阅读(25)  评论(0)    收藏  举报