对于一个学校项目,我已经开始制作一个数独求解器程序,这就是我到目前为止为入门系统提出的。唯一的问题是,每当我输入问题后使用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();
}
}
答案 0 :(得分:1)
问题在于:
} else {
redirectChoice(choice);
new Main();
}
用户选择加载电路板后,它就会被加载
您正在创建一个新的Main
对象以再次显示该菜单。事情就是这样
该板已加载到“旧”Main
对象,因此当用户想要显示时
它是空的 - 因为它是新创建的Main
对象的板。
答案 1 :(得分:0)
像这样的构造函数应该可以工作:
Main() {
initProblem();
do {
int choice = useMenu();
redirectChoice(choice);
}
while (choice != 0);
closeProgram();
}