猿问

我如何计算在没有无限循环的情况下达到所需余额的年数?

我目前正在为大学做软件基础教程,而我目前仍在解决这个问题。


“计算每年在银行帐户中获得指定金额的款项所需的年数,假设在每年年底支付利息,并且不提取任何款项。该程序应提示用户输入当前余额以磅为单位,所需的余额以磅为单位,利率以百分比表示。然后,它应计算并输出达到所需余额所需的年数,并在每年年底输出当前余额。”


到目前为止,我已经尝试过了,也尝试过移动变量,但是它始终会导致无限循环。如何停止呢?


package interest;


import java.util.Scanner;

public class Interest {



    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("hello what is your current balance ?");

        double balance = input.nextDouble();


        System.out.println("What is your required Balance");

        Double requiredBal = input.nextDouble();


        System.out.println("what is your current interest rate in %");

        double interest = input.nextDouble();

        int years = 0;

        double totalbalance=0;


        do {


            double intbalance = (balance /100) * interest;

            totalbalance = balance + intbalance;

            years ++;

            System.out.println("your balance after " + years +" years = " + totalbalance);


        }while(totalbalance <= requiredBal);

        System.out.println("it will take" + years + " years to get to " + requiredBal);

    }


}

它应该是这样的


enter current balance

100

enter required balance

200

enter interest rate   

10

balance after 1 year = 110.0 

balance after 2 year = 120.0  

balance after 3 year = 130.0 

balance after 4 year = 140.0

balance after 5 year = 150.0 

balance after 6 year = 160.0 

balance after 7 year = 170.0

balance after 8 year = 180.0

balance after 9 year = 190.0 

balance after 10 year = 200.0

It will take 10 years to reach the required balance.


UYOU
浏览 145回答 2
2回答

弑天下

让我们做一些婴儿数学。指数增长被定义为y=a(1+r)^x其中a是初始值和r是的兴趣(0和1之间)。此功能可映射X年的银行帐户余额。我们想要逆。如果对这个方程求逆,则得到y=logx/(log[a(1+r)]。x如果您可以进行一点数学运算,它将告诉您多少年才达到不需要循环的平衡。
随时随地看视频慕课网APP

相关分类

Java
我要回答