首页 > 解决方案 > 如何在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);
           }
  }

标签: javawhile-loop

解决方案


推荐阅读