在运行时存储数组

Store array at runtime

我是编程的绝对初学者,正面临着我的第一个大任务。 任务是仓库管理。所有新输入的框都应在 运行 时间存储在一个数组中。 问题是我可以创建一个新箱子,但它会被覆盖或无法保存。 我的想法是,每次输入成功我都调用运行函数。

import java.util.Arrays;

public class lager {
    static int parser (String value) {
        if (value == null) {
            return -1;
        } else {
            return Integer.parseInt(value);
        }
    }

    static void run (int[][]chests, int chestAmount, int maxStorage) {
        //Fallunterscheidung
        String option = JOptionPane.showInputDialog("What do you want to do (Type: 1-5)? \n" +
                "\n" +
                " 1 - Create chest \n" +
                " 2 - Delete chest \n" +
                " 3 - Change chest \n" +
                " 4 - Show chest \n" +
                " 5 - Show all");
        System.out.println(chestAmount);

        int optionParsed = parser(option);

        if (optionParsed > 5){
            System.out.println("Input incorrect or process aborted!");
            run(chests, chestAmount, maxStorage);
        } else if (optionParsed < 1){
            System.out.println("Input incorrect or process aborted!");
            return;
        } else {
            if (optionParsed == 1){
                boolean response = create(chests, chestAmount, maxStorage);
                if (!response){
                    System.out.println("Input incorrect or process aborted!");
                } else {
                    System.out.println("Process successfully completed!");
                }
            } else if (optionParsed == 2){
                boolean response = delete(chests, chestAmount, maxStorage);
                if (response){
                    System.out.println("Input incorrect or process aborted!");
                } else {
                    System.out.println("Process successfully completed!");
                }
            }
        }
        run(chests, chestAmount, maxStorage);
    }

    static boolean create (int[][]chests, int chestAmount, int maxStorage){
        if (chestAmount > maxStorage - 1) {
            System.out.println("The storage is already full!");
            return false;
        } else {
            int length = Integer.parseInt(JOptionPane.showInputDialog("How long is the chest in centimeters? (e.g. 115)"));
            int width = Integer.parseInt(JOptionPane.showInputDialog("How wide is the chest in centimeters? (e.g. 115)"));
            int height = Integer.parseInt(JOptionPane.showInputDialog("How high is the chest in centimeters? (e.g. 115)"));

            //THIS IS PROBABLY WHERE THE ERROR OCCURS
            chests[chestAmount] = new int[]{length, width, height};
            chestAmount++; //Count up help variable
            System.out.println(chestAmount);

            System.out.println("Stock " + chestAmount + " / " + maxStorage);
            System.out.println(Arrays.toString(chests[chestAmount - 1]));
            return true;
        }
    }

    static boolean delete (int[][]chests, int chestAmount, int maxStorage){
        String removeChest = JOptionPane.showInputDialog("Which chest do you want to delete? 1 - " + chestAmount);
        int removeChestParsed = parser(removeChest);

        if (removeChestParsed > chestAmount || removeChestParsed < 1){
            System.out.println("Chest not available.");
            return false;
        } else {
            chestAmount--;
            chests[removeChestParsed - 1] = chests[chestAmount];
            chests[chestAmount] = null;
            return true;
        }
    }


    public static void main(String[] args) {
        int maxStorage = 75;
        int[][] chests = new int[maxStorage][3]; //TODO: Wert 75 dynamisch anpassbar -- Fertig

        //Hilfsvariable
        int chestAmount = 0;

        run(chests, chestAmount, maxStorage);
    }
}


问题已解决。在 class Lager 而不是 main 方法中定义变量 chestAmount 之后,一切都按预期进行。