代码错误-Java储蓄帐户

代码错误-Java储蓄帐户,java,Java,输出混乱,将我的所有余额转换为0.000000 这是我的密码: package savingsaccountclass; import java.util.Scanner; public class SavingsAccountClass { public static void main(String[] args) { double annualInterestRate; double savingsBalance; d

输出混乱,将我的所有余额转换为0.000000

这是我的密码:

package savingsaccountclass;

import java.util.Scanner;

public class SavingsAccountClass 
{
    public static void main(String[] args) 
    {
        double annualInterestRate;
        double savingsBalance;
        double[] postInterestBalance = new double[100];
        int counter = 0;

        Scanner entry = new Scanner(System.in);

        System.out.println("Enter the current annual interest rate");
        annualInterestRate = entry.nextDouble();

        System.out.println("Enter the current balance.");
        savingsBalance = entry.nextDouble();

        while (counter < 12)
        {
            postInterestBalance[counter] = calculateMonthlyInterest(savingsBalance, annualInterestRate);
            System.out.printf("After Month %d. %f\n", counter + 1, postInterestBalance[counter]);
            counter++;
        }
    }

    public static double calculateMonthlyInterest(double balance, double interest)
    {
        double[] array = new double[100];
        int c = 0;
        double done = (balance * (interest/12));
        while (c < 12)
        {
            array[c] = (((c + 1) * done) + balance);
            c++;
        }
        return array[c];
    }
}
如果有人能让我知道为什么所有东西都转换成0,我会非常感激。
谢谢:)

您正在填写数组中的第一个
12
余额,但您返回的
数组[12]
,它从未分配过,因此是
0

Return
array[c-1]
返回数组中最后填充的元素

此外,您的
1
利率被解释为100%,即100/12,或每月加8 1/3%。将利率除以
100
,将百分比转换为所需的小数


此外,您当前没有计算复利。您当前正在计算初始余额
100
上当前每个“月”的利息。相反,你需要根据上个月的余额而不是初始余额来计算每月利息,使用
数组[c-1]
来访问上个月的余额。

但是当它添加余额时,它仍然会高于0,对吗?@RomainHippeau不,利息是
双倍的
。我尝试将利息设置为100,它仍然不起作用。如果是我,我就认为我妻子把它都花光了…………在你的最后一步中,你增加了c,并返回了错误的索引!我在上一个return语句之前做了c=0,现在只返回第一个月。如何使它返回第一个月、第二个月、第三个月……等等?谢谢你的帮助,我真的很感激!
run:
Enter the current annual interest rate
1
Enter the current balance.
100
After Month 1. 0.000000
After Month 2. 0.000000
After Month 3. 0.000000
After Month 4. 0.000000
After Month 5. 0.000000
After Month 6. 0.000000
After Month 7. 0.000000
After Month 8. 0.000000
After Month 9. 0.000000
After Month 10. 0.000000
After Month 11. 0.000000
After Month 12. 0.000000
BUILD SUCCESSFUL (total time: 1 second)