java - 如何在java中修复这个复利程序代码?
问题描述
我仍然是一个初学者,我正在尝试解决这个不断发生的问题。从本质上讲,目标是使用循环而不是方程来确定初始投资需要多长时间才能翻倍。该程序在每年复合时起作用,在每天或每月复合时不起作用。它以 200 年之类的长得可笑的时间段回答,我不知道我做错了什么,我非常感谢反馈。再次感谢你!
这是我下面的代码:
import java.util.*;
import java.io.*;
class Main
{
public static void main(String[] args)
{
int year = 0;
Scanner kbReader = new Scanner(System.in);
System.out.print("Enter account balance: $");
double balance = kbReader.nextDouble();
System.out.print("Enter interest rate as a decimal: ");
double interestRate = kbReader.nextDouble();
System.out.print("Enter period: ");
int period = kbReader.nextInt();
double targetBalance = 2 * balance;
while(balance < targetBalance)
{
double interest = balance * (interestRate/period);
balance = balance + interest;
year++;
}
System.out.println("The time it takes to double : " + year + " years");
System.out.printf("The final balance: $%.2f",balance);
}
}
解决方案
推荐阅读
- docker - 如何为非 docker Jenkins master 创建 docker slave 构建
- php - 提交表单按钮后重定向到 2 个不同的页面
- python - 预期 2 个值得到一个函数
- cassandra-3.0 - 如何在 Embedded-Cassandra 中获取特定版本的 Cassandra
- sql - 为“stat”多次指定了“30”列
- java - 为什么即使启用了 Wi-Fi 呼叫,方法 telephonyManager.getVoiceNetworkType() 也会返回 Network_LTE 的值?
- c# - AWS Lambda 函数在 .Net 核心中调用自身
- typescript - 打字稿什么是更好的方法类或接口
- asp.net - 如何从 ajax 选项卡容器中获取客户端 ID
- mysql - mysql中只有一条记录的“limit 10”和“limit 100”有什么区别吗?