英文:
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("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);
}
}
</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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论