净现值代码

import java.util.Scanner;

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("请输入成本");
double chengben = sc.nextDouble();
System.out.println("请输入贴现率");
double rate = sc.nextDouble();
System.out.println("请输入年限");
int year = sc.nextInt();
//定义一个数组
double[] flows = new double[year];
for (int i = 0; i < year; i++) {
System.out.println("请输入第" + (i + 1) + "年的钱");
flows[i] = sc.nextDouble();

}
double npy = 0;
npy = npvjisuan(chengben, flows, rate, year);


double originalValue = npy; // 需要四舍五入的原始值
double roundedValue = Math.round(originalValue * 100.0) / 100.0; // 进行四舍五入并保留两位小数


System.out.println(roundedValue);


}

public static double npvjisuan(double chengben, double[] liu, double rate, int year) {
double npv = chengben;
for (int i = 0; i < year; i++) {
npv += liu[i] / Math.pow(1.0 + rate, i + 1);

}


return npv;
}


}
posted @   想不起来好名字  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2023-03-12 3.12
点击右上角即可分享
微信分享提示