0

请帮我。基本上,程序应该要求用户输入一个代表储蓄账户初始余额的数字。将此数字分配给一个名为 balance 的双变量。要求一个代表帐户年利率(百分比)的数字。将此数字除以 100.0 并将其分配给名为 rate 的双精度变量。我必须使用循环来更新余额,因为它每年都在变化。我被困在那部分。这是我到目前为止的代码:

   public static void calcInterest(){
System.out.println("Please enter the account balance : ");
     System.out.println("Please enter the annual interest rate : ");
     System.out.println("Please enter the number of years : ");
     Scanner input = new Scanner (System.in);
     double balance = input.nextDouble();
     double y = input.nextDouble();
     double rate = (y/100);
     int years = input.nextInt();


     }
4

1 回答 1

1

在这种情况下没有充分的理由使用循环,但我想这是出于学习目的。您可以创建一个循环来计算每年的新余额,如下所示:

for(int i = years; i > 0; i--)
    balance = balance * y;

或者使用 Math.pow(这遵循公式 startvalue * rate of change^time = result):

balance = balance * Math.pow(y, years);
于 2016-01-17T18:34:38.510 回答