如何将double存储到数组中并对其进行修改

时间:2019-05-06 14:48:58

标签: java joptionpane

我要创建一个银行程序。我应该可以存款,提款,查看余额,然后退出。

我的问题是我们还没有遍历数组,这就是我要使用的数组。当我用[0]初始化数组时,因为它一次仅是一种数据类型,因此我收到:  线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:0

所以我的问题是,在修改余额直到用户退出之前,我如何能够做到这一点。我有点卡住...

我们会尽力帮助您,我会为混乱的代码预先提出要求。

谢谢大家的帮助。我确实放了:double [] balance = new double [1];

但是现在我回到了我无法继续修改​​具有更多存款和提款的数组的问题。我将其初始化回0.0。

有人能指出我正确的方向吗?

主类:

public class Project3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Introduction();
        Banking obj1 = new Banking();

    }
    public static void Introduction() 
    {

        //this message tells the user exactly what this program does.

        JOptionPane.showMessageDialog(null,
            "This is a small conversion program which allows you to do "    
                           + "\nDeposit "
                           + "\nWithdrawl."
                           + "\nView Balance."
                           + "\nExit",
                   "Conversion Calculator",
                   JOptionPane.INFORMATION_MESSAGE);
    }
}

班级:

public class Banking 
{
    double deposit;
    double newbalance;

    double[] balance = new double[0];

    public Banking() 
    {
        Menu();
    }
    public void Menu() 
    {
        JDialog.setDefaultLookAndFeelDecorated(true);                             
        Object[] selectionValues = { "Deposit", "Withdrawl"};
        String initialSelection = "Deposit";
        Object selection = JOptionPane.showInputDialog(null, "Would you like to "
               + "do today?",
            "Home Banking Selection Screen", JOptionPane.PLAIN_MESSAGE, null, selectionValues, initialSelection);      
        // If statements used to call the method selected      
        if (selection == "Deposit")
            Deposit();
        if (selection == "Withdrawl")
            Withdrawl();
    }
    public void Deposit() 
    {
         balance[0] = Double.parseDouble(JOptionPane.showInputDialog(null,
                "Please enter the total number of degrees in Fahrenheit to be Converted into Celcius? and press 'Ok'",
                "Deposit",
                JOptionPane.PLAIN_MESSAGE));  

         JOptionPane.showMessageDialog(null, 
                "You doposited $ " +balance+ "\n You now have $" ,
                "The End",
                JOptionPane.PLAIN_MESSAGE);


         JDialog.setDefaultLookAndFeelDecorated(true);                             
        Object[] selectionValues = { "Continue with more transactions", "Exit"};
        String initialSelection = "Deposit";
        Object selection = JOptionPane.showInputDialog(null, "Would you like to "
               + "do today?",
            "Home Banking Selection Screen", JOptionPane.PLAIN_MESSAGE, null, selectionValues, initialSelection);      
        // If statements used to call the method selected      
        if (selection == "Continue witl more transactions")
            Menu();
        if (selection == "Exit")
            Exit();
    }
    public void Withdrawl() 
    {
        JOptionPane.showMessageDialog(null, 
                "          Will be Withdrawl!", //Message to tell the user the program has ended
                "2",
                JOptionPane.PLAIN_MESSAGE);   
    }
    public void Exit() 
    {
        JOptionPane.showMessageDialog(null, 
                "          Thank you and have a great day!", //Message to tell the user the program has ended
                "The End",
                JOptionPane.PLAIN_MESSAGE);   
    }
}

3 个答案:

答案 0 :(得分:0)

double[] balance = new double[0];不创建0数组 的。

例如:您的数组看起来不是这样的:

[0,0,0,0,0,0,0,0,0]

相反,您的数组如下所示:

[]

Object obj = new Object[x]将创建一个长度为x的数组。但是,即使那样,数组中的项目也不会初始化。您必须手动将每个设置为 0

答案 1 :(得分:0)

您有一个大小为0的双精度数组:

double[] balance = new double[0];

然后您要访问它的第一个(不存在的元素):

balance[0] = Double.parseDouble(JOptionPane.showInputDialog(null,
            "Please enter the total number of degrees in Fahrenheit to be Converted into Celcius? and press 'Ok'",
            "Deposit",
            JOptionPane.PLAIN_MESSAGE));  

这就是抛出ArrayIndexOutOfBoundsException: 0的原因。

只需使用double[] balance = new double[1];
这是您要创建的数组的大小^

默认情况下,每个元素都将以0.0初始化。

UPD:实际上,您可能希望尝试添加一些检查以确保所触摸的用户确实存在并且余额已正确设置。

答案 2 :(得分:0)

您可以通过以下方式初始化数组: false

这时数组的大小将为零。

但是,根据您的问题,我认为您不需要和安排。 只需这样声明平衡:

double[] balance = {};

存款时: double balance = 0.00d;

退出时: balance = balance + depositAmount;