在运行时存储数组

huangapple go评论64阅读模式
英文:

Store array at runtime

问题

import javax.swing.JOptionPane;
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);
    }
}
英文:

I am an absolute beginner in programming and am facing my first big tasks.
The task is a warehouse management. All newly entered boxes should be stored in an array at runtime.
The problem is that I can create a new chest, but it will be overwritten or not saved.
My idea is that I call the run function every time an entry was successful.

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(&quot;What do you want to do (Type: 1-5)? \n&quot; +
&quot;\n&quot; +
&quot; 1 - Create chest \n&quot; +
&quot; 2 - Delete chest \n&quot; +
&quot; 3 - Change chest \n&quot; +
&quot; 4 - Show chest \n&quot; +
&quot; 5 - Show all&quot;);
System.out.println(chestAmount);
int optionParsed = parser(option);
if (optionParsed &gt; 5){
System.out.println(&quot;Input incorrect or process aborted!&quot;);
run(chests, chestAmount, maxStorage);
} else if (optionParsed &lt; 1){
System.out.println(&quot;Input incorrect or process aborted!&quot;);
return;
} else {
if (optionParsed == 1){
boolean response = create(chests, chestAmount, maxStorage);
if (!response){
System.out.println(&quot;Input incorrect or process aborted!&quot;);
} else {
System.out.println(&quot;Process successfully completed!&quot;);
}
} else if (optionParsed == 2){
boolean response = delete(chests, chestAmount, maxStorage);
if (response){
System.out.println(&quot;Input incorrect or process aborted!&quot;);
} else {
System.out.println(&quot;Process successfully completed!&quot;);
}
}
}
run(chests, chestAmount, maxStorage);
}
static boolean create (int[][]chests, int chestAmount, int maxStorage){
if (chestAmount &gt; maxStorage - 1) {
System.out.println(&quot;The storage is already full!&quot;);
return false;
} else {
int length = Integer.parseInt(JOptionPane.showInputDialog(&quot;How long is the chest in centimeters? (e.g. 115)&quot;));
int width = Integer.parseInt(JOptionPane.showInputDialog(&quot;How wide is the chest in centimeters? (e.g. 115)&quot;));
int height = Integer.parseInt(JOptionPane.showInputDialog(&quot;How high is the chest in centimeters? (e.g. 115)&quot;));
//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(&quot;Stock &quot; + chestAmount + &quot; / &quot; + maxStorage);
System.out.println(Arrays.toString(chests[chestAmount - 1]));
return true;
}
}
static boolean delete (int[][]chests, int chestAmount, int maxStorage){
String removeChest = JOptionPane.showInputDialog(&quot;Which chest do you want to delete? 1 - &quot; + chestAmount);
int removeChestParsed = parser(removeChest);
if (removeChestParsed &gt; chestAmount || removeChestParsed &lt; 1){
System.out.println(&quot;Chest not available.&quot;);
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);
}
}
</details>
# 答案1
**得分**: 0
问题已解决。在类"大储存器"(Lager)中定义变量"chestAmount",而不是在主方法中定义,一切都按预期进行。
<details>
<summary>英文:</summary>
The problem was fixed. After I defined the variable chestAmount in the class Lager and not in the main method, everything went as desired.
</details>

huangapple
  • 本文由 发表于 2020年9月28日 13:50:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64096601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定