如何以递增方式在数组中存储double

时间:2014-06-23 15:24:09

标签: java

这是我的第一篇文章,希望有人可以提供帮助。我已经苦苦挣扎多年才能理解我所遇到的问题,看起来好像我已经做好了一切,但问题仍然存在。我编写了一个简单的帐户程序,并希望将每个事务后的新余额存储到数组索引中。从打印出来的余额中我可以看到它正在正确地添加和减少,但是它使用新的余额而不是每个增量索引更新每个单个数组索引。谁能发现我的错误。感谢您对此提供任何帮助。

理查德

public class TestAppAccount {
    public static void main(String args[]) {

    int count = 0;

    do {
        AppAccount.transaction();
        AppAccount.storeBalance();
        AppAccount.printBalance();
        count++;
        //Account1.printBalance2();
    } while (count <= 100);

}

public class AppAccount {

    public static double[] currentBalance = new double[100];
    static Scanner keyboard = new Scanner(System.in);
    static double balance;
    static int transaction;


    public static void transaction() {
        System.out.println(" Press 1 to add funds, 2 to withdraw ");
        transaction = keyboard.nextInt();

        if (transaction == 1) {
            System.out.println("Enter an amount to add ");
            double amount = keyboard.nextInt();
            double newBalance = balance + amount;
            balance = newBalance;
        } else

        {
            System.out.println("Enter an amount to withdraw");
            double amount = keyboard.nextInt();
            double newBalance = balance - amount;
            balance = newBalance;
        }
    }

    public static void storeBalance() {
        for (int i = 0; i < currentBalance.length; i++) {
            currentBalance[i] = balance;

        }

    }

    public static void printBalance() {
        System.out.println("Balance: " + balance);
        System.out.println("Balance: " + currentBalance[3]);//testing by printing 3rd index
    }
}

5 个答案:

答案 0 :(得分:1)

您的storeBalance()方法正在遍历整个数组并更新值。如果您只想更新一个值,则需要跟踪您写入的位置,然后只写入该位置。

e.g。

而不是

public static void storeBalance() {
    for (int i = 0; i < currentBalance.length; i++) {
        currentBalance[i] = balance;
    }
}

试试这个:

static int lastStored = 0;
public static void storeBalance() {
    currentBalance[lastStored++] = balance;
}

答案 1 :(得分:0)

    for (int i = 0; i < currentBalance.length; i++) {
    currentBalance[i] = balance;

    }

for循环将数组中的每个值设置为balance的值。如果每个索引都应该是一个不同的帐户,我建议将索引传递给每个函数。

答案 2 :(得分:0)

问题在于:

public static void storeBalance() {
    for (int i = 0; i < currentBalance.length; i++) {
        currentBalance[i] = balance;
    }  
}

for循环将逐个遍历数组中的每个元素,并使用当前balance覆盖每个值。

有几种方法可以解决这个问题。我的第一反应是创建一个变量,告诉你要写入的下一个索引:

static int nextIndex = 0;

然后storeBalance()看起来像这样:

public static void storeBalance() {
    currentBalance[nextIndex] = balance;
    nextIndex++;
}

答案 3 :(得分:0)

问题是您执行for循环并更新每个值。您只想在调用storeBalance()时更新一次。跟踪i作为static变量:

static int i = 0;

然后这样做:

public static void storeBalance() {
    if(i >= currentbalance.length)
        i = 0;//restart value so it doesnt do array out of bounds
    currentbalance[i] = balance;//update a single value
    i++;//increment for next call
}

确保您进行越界检查。这就是其他答案忘记检查的内容。当然,这会过度写旧值,因此您可以选择做其他事情(似乎适合您的要求)。

答案 4 :(得分:0)

停止假装java是c。 Java不是c。 Java喜欢对象。 使用对象。 避免使用数组。

以下是一些代码:

// start Main.java file

public class Main
{
    public static void main(String args[])
    {
        int count = 0;

        do
        {
            AppAccount.transaction();
            AppAccount.storeBalance();
            AppAccount.printBalance();
            count += 1;
        }
        while (count <= 4);

    }
}

// start AppAccount.java file

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AppAccount
{
    private static List<Double> currentBalanceList = new ArrayList<Double>();
    private static Scanner keyboard = new Scanner(System.in);
    private static double runningBalance;
    private static int transaction;

    public static void printBalance()
    {
        System.out.println("Running Balance: " + runningBalance);
    }

    public static void storeBalance()
    {
        currentBalanceList.add(runningBalance);
    }

    public static void transaction()
    {
        double amount;

        System.out.println(" Press 1 to add funds, 2 to withdraw ");
        transaction = keyboard.nextInt();

        if (transaction == 1)
        {
            System.out.println("Enter an amount to add ");
            amount = keyboard.nextDouble();
            runningBalance = runningBalance + amount;
        }
        else
        {
            System.out.println("Enter an amount to withdraw");
            amount = keyboard.nextDouble();
            runningBalance = runningBalance - amount;
        }
    }
}