2013-07-01 53 views
0

對於一個學校項目,我已經開始製作一個數獨求解器程序,這就是我目前爲入門系統所提出的。唯一的問題是,每當我在輸入問題後使用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(); 
} 

}

+3

這不是「調試我的代碼爲我」的網站。使用調試器來本地化和理解你的錯誤。問一些具體但更一般的問題。 – AlexR

+0

所有的調試器只是給我說AQAConsole2013是無效的,它並不是孤立的,我不知道這個問題會是什麼。這似乎是最好的地方問。 – ADwards

+0

事實上,學會在你的IDE中使用調試器(或者在你的代碼中放置日誌語句,例如在輸入'displayProblem()'時顯示'prob'數組的內容)。 – herman

回答

1

的問題是在這裏:用戶進行

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

後選擇加載板,它得到的加載 你正在創建一個新的Main對象,以顯示菜單。事情是, 板被加載到'舊'Main對象,所以當用戶想要顯示 板時,它是空的 - 因爲它是來自新創建的Main對象的板。

0

像這樣的構造應該工作:

Main() { 
    initProblem(); 
    do { 
     int choice = useMenu(); 
     redirectChoice(choice); 
    } 
    while (choice != 0); 
    closeProgram(); 
}