我目前正在为大学做软件基础教程,而我目前仍在解决这个问题。“计算每年在银行帐户中获得指定金额的款项所需的年数,假设在每年年底支付利息,并且不提取任何款项。该程序应提示用户输入当前余额以磅为单位,所需的余额以磅为单位,利率以百分比表示。然后,它应计算并输出达到所需余额所需的年数,并在每年年底输出当前余额。”到目前为止,我已经尝试过了,也尝试过移动变量,但是它始终会导致无限循环。如何停止呢?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 balance100enter required balance200enter interest rate 10balance after 1 year = 110.0 balance after 2 year = 120.0 balance after 3 year = 130.0 balance after 4 year = 140.0balance after 5 year = 150.0 balance after 6 year = 160.0 balance after 7 year = 170.0balance after 8 year = 180.0balance after 9 year = 190.0 balance after 10 year = 200.0It will take 10 years to reach the required balance.
2 回答
弑天下
TA贡献1818条经验 获得超8个赞
让我们做一些婴儿数学。
指数增长被定义为y=a(1+r)^x
其中a
是初始值和r
是的兴趣(0和1之间)。此功能可映射X年的银行帐户余额。我们想要逆。
如果对这个方程求逆,则得到y=logx/(log[a(1+r)]
。x
如果您可以进行一点数学运算,它将告诉您多少年才达到不需要循环的平衡。
添加回答
举报
0/150
提交
取消