我目前正在为大学做软件基础教程,而我目前仍在解决这个问题。
“计算每年在银行帐户中获得指定金额的款项所需的年数,假设在每年年底支付利息,并且不提取任何款项。该程序应提示用户输入当前余额以磅为单位,所需的余额以磅为单位,利率以百分比表示。然后,它应计算并输出达到所需余额所需的年数,并在每年年底输出当前余额。”
到目前为止,我已经尝试过了,也尝试过移动变量,但是它始终会导致无限循环。如何停止呢?
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.
弑天下
相关分类