java - 投资程序,没有给出最终结果所需的正确计算
问题描述
示例输出 1:
输入您的姓名:鲍勃·约翰逊
年龄:20
每月供款:300 美元
年回报率(%):8
投资年限:10
开始投资年龄:20
提款前的年数:40
提款时的总投资为:596,219.72 美元
示例输出 2:
输入您的姓名:瑞奇·特罗特
年龄:20
每月供款:300 美元
年回报率(%):8
投资年限:30
开始投资年龄:30
提款前的年数:40
提款时的总投资为:443,548.84 美元
代码仅给出第一个和第二个周期的正确值。最终值为 2.49E163
class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scan.nextLine();
System.out.print("Age: ");
double age = scan.nextInt();
System.out.print("Monthly Contribution: ");
double month = scan.nextDouble();
System.out.print("Anual Rate of Return (%): ");
double rate = scan.nextInt();
rate = (rate/100.0);
System.out.print("Years of Investing: ");
double years = scan.nextInt();
System.out.print("Start of Investing Age: ");
double start = scan.nextInt();
System.out.print("Years until withdrawing Money: ");
double yearsuntil = scan.nextInt();
double cont = month;
int year = 12;
double fv = 0.0;
for (int i = 0; i <= yearsuntil; i++)
{
for (int j = 0; j <= year; j++)
{
//Future Value = Monthly Contribution * Annual Rate of Return / 12 + Monthly Contribution
fv = ((cont * .08)/12) + cont;
cont += fv;
}
}
NumberFormat total = NumberFormat.getCurrencyInstance();
System.out.println("Total Investments at Withdraw time will be: " + total.format(fv));
}
}
解决方案
推荐阅读
- android - Lint 在组装发布目标时发现了致命错误
- reactjs - 有没有办法将 CRA 与 PHP 后端一起使用?
- ruby - nokogiri 的语法错误,意外的“。” 在 nokogiri::CSS 对象之后
- azure - Azure 网站无法与第三方 API 通信
- python - 如何将 mako 中的模板拆分为多个文件/目录?
- spring - 如何使用spring和hibernate从数据库中检索数据并在jsp中打印?
- python - ML 引擎从作曲家失败 - 未知名称“python-version”
- r - 在单个命令中绘制每一行的数据
- kubernetes - kube-dns 服务发现无法发现服务的端口号
- docker - 这个 Dockerfile 语句有什么问题?我应该使用哪一个?