gpt4 book ai didi

java - 程序期间重置全局字符数组的值

转载 作者:行者123 更新时间:2023-12-01 14:22:49 25 4
gpt4 key购买 nike

对于一个学校项目,我已经开始制作一个数独解算器程序,这就是我迄今为止为入门系统提出的方案。唯一的问题是,每当我在输入问题后使用 displayProblem 方法时,它都会显示完全空白(即所有字符都设置为“0”)。有人能帮我解决这个问题吗?

public class Main {
AQAConsole2013 c = new AQAConsole2013();
char[] prob = new char[81];

Main() {
initProblem();
int choice = useMenu();
if (choice == 0) {
closeProgram();
} else {
redirectChoice(choice);
new Main();
}
}

public void initProblem() {
for (int i = 0; i != 81; i++) {
prob[i] = '0';
}
}

public int useMenu() {
displayMenu();
return getChoice();
}

public void displayMenu() {
c.println("Sudoku Solver:");
c.println("---------------------------");
c.println("1. Load Problem");
c.println("2. Display Current Problem");
c.println("3. Solve by Brute Force");
c.println("4. Help");
c.println("0. Exit Program");
}

public int getChoice() {
int x = -1;
while (x > 4 || x < 0) {
x = c.readInteger("\nChoose an option: ");
if (x > 4 || x < 0) {
c.println("That is not a valid choice.");
}
}
return x;
}

public void redirectChoice(int x) {
switch (x) {
case 1:
loadProblem();
break;
case 2:
displayProblem();
break;
case 3:
break;
case 4:
break;
}
}

public void loadProblem() {
c.println("\nTo enter a problem into the application, label each column A-I from left to right, and label each row 1-9 from top to bottom.\nLabel unknowns with a hyphen (-)\nAny multiple digit number entered will be saved as just the first digit (23 would be saved as just 2)\nIf you make a mistake, you can type \'r\' to start again.\n");
populateProblem();
}

public void populateProblem() {
char[] x = new char[81];
char[] iChar = new char[10];
for (int i = 0; i != 9; i++) {
iChar = numToChar(i);
for (int j = 1; j != 10; j++) {
while (x[(i * 9 + j) - 1] != '1' && x[(i * 9 + j) - 1] != '2'
&& x[(i * 9 + j) - 1] != '3'
&& x[(i * 9 + j) - 1] != '4'
&& x[(i * 9 + j) - 1] != '5'
&& x[(i * 9 + j) - 1] != '6'
&& x[(i * 9 + j) - 1] != '7'
&& x[(i * 9 + j) - 1] != '8'
&& x[(i * 9 + j) - 1] != '9'
&& x[(i * 9 + j) - 1] != '-'
&& x[(i * 9 + j) - 1] != 'r') {
try {
x[(i * 9 + j) - 1] = c
.readChar("Enter the value for square "
+ iChar[i] + j + ": ");
} catch (StringIndexOutOfBoundsException e) {
c.println("That is not a valid value.");
}
}
if (x[(i * 9 + j) - 1] != '1' && x[(i * 9 + j) - 1] != '2'
&& x[(i * 9 + j) - 1] != '3'
&& x[(i * 9 + j) - 1] != '4'
&& x[(i * 9 + j) - 1] != '5'
&& x[(i * 9 + j) - 1] != '6'
&& x[(i * 9 + j) - 1] != '7'
&& x[(i * 9 + j) - 1] != '8'
&& x[(i * 9 + j) - 1] != '9'
&& x[(i * 9 + j) - 1] != '-'
&& x[(i * 9 + j) - 1] != 'r') {
c.println("That is not a valid value.");
} else if (x[(i * 9 + j) - 1] == 'r') {
c.println("------------------------------");
populateProblem();
}
}
}
for (int k = 0; k != 81; k++) {
prob[k] = x[k];
}
c.println("");
}

public char[] numToChar(int x) {
char[] a = new char[10];
for (int i = 0; i != 10; i++) {
a[i] = (char) (i + 65);
}
return a;
}

public void displayProblem() {
c.print("");
for (int i = 0; i != 9; i++) {
c.println("\t");
if (i == 0 || i == 3 || i == 6) {
c.println("------------------");
}
for (int j = 1; j != 10; j++) {
char y = ' ';
if (j == 0 || j == 3 || j == 6 || j == 9) {
y = '|';
}
c.print(prob[(i * 9 + j) - 1]);
c.print(y);
}
}
c.println("\n------------------\n");
}

public void closeProgram() {
c.println("\nGoodbye.");
System.exit(0);
}

public static void main(String[] args) {
new Main();
}

}

最佳答案

问题出在这里:

} else {
redirectChoice(choice);
new Main();
}

在用户选择加载板并且它被加载之后您正在创建一个新的 Main 对象以再次显示菜单。事情是这样的板已加载到“旧”Main 对象,因此当用户想要显示时板,它是空的 - 因为它是来自新创建的 Main 对象的板。

关于java - 程序期间重置全局字符数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17404732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com