나는 스도쿠 해결 프로그램을 쓰고 있어요,하지만 나는 다음과 같은 오류가 발생하고 있습니다를 찾을 수 없습니다. 내 프로그램의 코드는 다음과 같습니다 :는 실수 (StackOverflowError가)
package code;
import java.awt.Color; //Import nötiger Klassen
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SOLVE implements ActionListener{
private int[][] matrix; //Eingelesenes Sudokufeld
private boolean[][]input; //Überfrüfungsfeld zur bestimmung von eingegebenen Zahlen
private JFrame fenster; //GUI Fenster
private JTextArea field[][]; //GUI Text Sudoku Feld
private JButton start; //Start Button
private JButton reset;
public void gui(){ //Erstellt die Grafische Oberfläche
Border b1 = BorderFactory.createLineBorder(Color.black);
//Erstellt Fenster
fenster = new JFrame();
fenster.setLocation(100, 100);
fenster.setSize(500, 320);
fenster.getContentPane().setLayout(null);
fenster.setTitle("Sudoku Solver");
//Erstellt Sudoku TextFelder
field = new JTextArea[9][9];
int x = 0; //x-Koordinate
for(int i=0;i<9;i++){
int y = 0; //y-Koordinate
for(int j=0;j<9;j++){
field[i][j] = new JTextArea();
field[i][j].setLocation((200+x),(30+y));
field[i][j].setSize(20, 20);
field[i][j].setText("0");
field[i][j].setBorder(b1);
field[i][j].setVisible(true);
fenster.add(field[i][j]);
if(j==2||j==5){
y=y+32;
}
else{
y=y+25;
}
}
if(i==2||i==5){
x=x+32;
}
else{
x=x+25;
}
}
//Erstellt Start Button
start = new JButton();
start.setText("Solve");
start.setSize(150,50);
start.setLocation(30,30);
start.addActionListener(this);
//Erstellt Reset Button
reset = new JButton();
reset.setText("Reset");
reset.setSize(150,50);
reset.setLocation(30,100);
reset.addActionListener(this);
//Fügt Komponenten zum Fenster hinzu
fenster.add(reset);
fenster.add(start);
fenster.setVisible(true);
}
public SOLVE(){ //Startet Klasse
gui();
matrix = new int[9][9];
input = new boolean[9][9];
}
public void copy(){ //Einlesen der TextFelder in die Matrix une Überprüfungs Matrix
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
matrix[i][j]=Integer.parseInt(field[i][j].getText());
if(matrix[i][j]!=0){
input[i][j]=false;
}
else{
input[i][j] = true;
}
}
}
}
public boolean check(int x,int y, int solution){ //Überprüfung ob Lösung möglich ist für eine bestimmte Zahl bei einem bestimmten Feld
boolean moeglich = false; //Insgesammt möglich
boolean wmoeglich = true; //Waagrecht möglich
boolean smoeglich = true; //Senkrecht möglich
boolean fmoeglich = true; //Feld möglich
//Überprüft waagrecht und senkrecht ob möglich
for(int i=0;i<9;i++){
if(matrix[x][i]==solution){
wmoeglich = false;
}
if(matrix[i][y]==solution){
smoeglich = false;
}
}
//Sucht nötiges Feld und überprüft dieses
//Oberste Reihe
if(x<3){
//linkes Feld
if(y<3){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//mittleres Feld
else if(y<6&& y>2){
for(int i=0;i<3;i++){
for(int j=3;j<6;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//rechtes Feld
else if(y<9 && y>5){
for(int i=0;i<3;i++){
for(int j=6;j<9;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
}
//mittlere Reihe
else if(x<6 && x>2){
//linkes Feld
if(y<3){
for(int i=3;i<6;i++){
for(int j=0;j<3;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//mittleres Feld
else if(y<6 && y>2){
for(int i=3;i<6;i++){
for(int j=3;j<6;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//rechtes Feld
else if(y<9 && y>5){
for(int i=3;i<6;i++){
for(int j=6;j<9;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
}
//unterste Reihe
else if(x<9 && x>5){
//linkes Feld
if(y<3){
for(int i=6;i<9;i++){
for(int j=0;j<3;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//mittleres Feld
else if(y<6 && y>2){
for(int i=6;i<9;i++){
for(int j=3;j<6;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//rechtes Feld
else if(y<9 && y>5){
for(int i=6;i<9;i++){
for(int j=6;j<9;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
}
//wenn eine Überprüfung fehlgeschlagen Lösung nicht möglich
if(smoeglich == true && wmoeglich == true && fmoeglich == true){
moeglich = true;
}
return moeglich;
}
public void search(int i, int j){ //Lösen des Sudokus (Eigentlich Rekursiv)
if(input[i][j]==true){ //Wenn keine menschliche eingabe
int l = 1;
for(l=1;l<10;l++){
if(check(i,j,l)==true){ //Wenn Überprüfung erfolgreich war
matrix[i][j]=l; //Schreibt Lösung in Matrix
field[i][j].setText(""+l); //Und Schreibt Lösung in TextFeld
//Nachfolger suchen
if(i<=7){
search(i+1,j);
}
else if(i==8 && j<=7){
search(i-8,j+1);
}
else{
break;
}
}
//Da Rekursion nicht funktionierte hier den Schritt zurück
else if(check(i,j,l)==false && l==9){
matrix[i][j]=0;
if(i>0){
search(i-1,j);
}
else if(i==0 && j>0){
search(i+8,j-1);
}
else{
break;
}
}
}
}
else{
//Nachfolger suchen
if(i<=7){
search(i+1,j);
}
else if(i==8 && j<=7){
search(i-8,j+1);
}
else{}
}
}
public void reset(){ //Resettet Fenster und Matrix
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
field[i][j].setText("0");
matrix[i][j]=0;
input[i][j]=true;
}
}
}
public void master(){ //Master Methode (Führt die nötigen Methoden nacheinander aus)
System.gc();
copy();
search(0,0);
}
public static void main(String[] args) { //Main Methode (Start Methode)
new SOLVE();
}
public void actionPerformed(ActionEvent evt){ //Button Abfrage
Object source = evt.getSource();
if(source==start){ //Wenn Button 'start' keklckt wird Methode main ausführen
master();
}
else if(source == reset){
reset();
}
}
}
이 오류의 원인은 무엇입니까? 코드의 주석이 독일어로되어있어서 유감이지만, 원한다면 영어로 번역 할 수 있습니다.
문제를 진단하기에 충분한 stacktrace를 게시하지 않았습니다. StackOverflowErrors를 사용하면 추적에서 전체 사이클을 찾아야합니다. –
'search()'가 재귀 적으로 자신을 호출하는 것처럼 보입니다. 내 추측은 너무 깊어서 스스로를 호출하고 스택 메모리를 소모한다는 것입니다. –
@stvcisco 아니, 그게 틀림 없어. stacktrace를보십시오. –