循环和存储输入的信息

时间:2015-10-19 21:47:52

标签: java loops

因此,我必须制作一个获得余额的银行帐户计划,并由用户输入的每个月询问他们当月的存款和取款,并在每个月从余额中添加或减少。

我让循环工作但我想知道如何存储我收到的数据,我需要将存款和取款分开存储。

import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class QueryBankAccount {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        String balance = JOptionPane.showInputDialog(frame, "Enter your Balance:");
        String interest = JOptionPane.showInputDialog(frame, "Enter your Interest:");
        String months = JOptionPane.showInputDialog(frame, "Enter your Months:");

        double balancedouble = Double.parseDouble(balance);
        double interestdouble = Double.parseDouble(interest);
        int monthsint = Integer.parseInt(months);

        int counter = 0;

        while (counter < monthsint){
            String Deposits = JOptionPane.showInputDialog(frame, "Enter your Deposits:");
            String Withdrawls = JOptionPane.showInputDialog(frame, "Enter your Withdrawls:");

            double depositsdouble = Double.parseDouble(Deposits);
            double withdrawlsdouble = Double.parseDouble(Withdrawls);

            counter++;
        }

1 个答案:

答案 0 :(得分:0)

如果您想将数值存储在固定数量的空间中,请使用数组,但如果您不知道尺寸并且希望它可以调整大小,请使用arraylist并将您的提款和/或存款添加到其中。例如:

double totalDep[] = new double[10]; //assuming total is 10 deposits
  while (counter < monthsint){
        String Deposits = JOptionPane.showInputDialog(frame, "Enter your Deposits:");
        String Withdrawls = JOptionPane.showInputDialog(frame, "Enter your Withdrawls:");

        double depositsdouble = Double.parseDouble(Deposits);
        double withdrawlsdouble = Double.parseDouble(Withdrawls);

        totalDep[counter] = depositsdouble; //store into array here

        counter++;
    }

同样地,你也可以为撤回做一个,这是一个arraylist版本:

ArrayList<Double> totalDep = new ArrayList<Double>(); 
 while (counter < monthsint){
        String Deposits = JOptionPane.showInputDialog(frame, "Enter your Deposits:");
        String Withdrawls = JOptionPane.showInputDialog(frame, "Enter your Withdrawls:");

        double depositsdouble = Double.parseDouble(Deposits);
        double withdrawlsdouble = Double.parseDouble(Withdrawls);

        totalDep.add(depositsdouble); 
        counter++;
    }