分配中的java.lang.StackOverflowError

时间:2014-03-21 16:51:14

标签: java variable-assignment

当我运行我的代码时,我得到了java.lang.StackOverflowError

如果有人能告诉我什么是错的,我会非常感激。

这是我的代码:

我的任务是制作数独游戏。 奇怪的是,当我调试它时,一切进展顺利,但我无法永远调试它。

需要大家的帮助:(

public static void main(String[] args) {
    System.out.println("Welcome to java Sudoku to start the game press enter");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    int AllNum=1000;
    TestRow=0;
    TestColumn=0;
    TheNumUPut=0;
    finish=false;

            while(AllNum>99){
                    AllNum =GivenANum();
                    if(AllNum>99)NumInPlace(AllNum);}//end while


            SolveSudoku();  
}// end main


public static void SolveSudoku() {

            if (TestRow==9&&TestColumn==0)PrintSudoku();

            if(finish==false){
                if (SolveOneCell()==true){
                    TestColumn++;
                    if(TestColumn==9) {TestColumn=0; TestRow++;}}
                else
                    ReturnToLast();

                    SolveSudoku();
            }

}// end Solve Sudoku


public static boolean SolveOneCell( ){// הפונקציה פותרת את הסודוקו לאחר שהמשתמש הכניס את המספרים שלו
                if(IsGiven()==true) return true;

                IncreaseNum();

                while (TheNumUPut<11){
                if(TheNumUPut>9){PutZero(); return false;}

                if(UCanPut()==true){Put();return true;}
                else
                    TheNumUPut++;}

    return true;
    }// end solve

public static boolean ReturnToLast() {
    while(1>0){
            if(TestColumn==0)
                {
                    TestColumn=8;
                    TestRow=TestRow-1;
                }
            else TestColumn=TestColumn-1;

            if(IsGiven()==false) break;
    }

    return true;
}// end return to NON last given



public static int GivenANum(){// הפונקציה קולטת מספר מהמשתמש
        int PutANum;
        System.out.println("Please enter a given number and its location ?");
        PutANum = reader.nextInt();
        return PutANum;
}// end givens

public static void NumInPlace(int TheNum){// הפונקציה שמה את המספר שנקלט במקום הנכון על הלוח
    Row = (TheNum/10)%10;
    Column= TheNum%10;
    TheGivenNumber= (TheNum/100)%10;
    TheBoard [Row][Column]=TheGivenNumber;  
    TheGivenBoard[Row][Column]=TheGivenNumber;
}// end NumInPlace



public static void Put(){// שם בתא את המספר
    if(TestRow>(-1)&&TestColumn>(-1)){
        TheBoard[TestRow][TestColumn]=TheNumUPut;   }

}// end Put



public static void PutZero(){// שם 0 בתא
    if(TestRow<9&&TestColumn<9)  TheBoard[TestRow][TestColumn]=0;   
}// end PutZero



public static void IncreaseNum(){// מגדילה את המספר ב1
    if(TestRow<9&&TestColumn<9)  TheNumUPut=TheBoard[TestRow][TestColumn]+1;    
}// end increase



public static boolean IsGiven(){// הפונקציה בודקת אם במקום יש מספר שהמשתמש הכניס
    if(TestRow>8 || TestColumn>8) return false;
    if (TheGivenBoard[TestRow][TestColumn]!=0) return true;
    return false;
}// end Not given


public static boolean UCanPut(){// הפונקציה בודקת אם ניתן להכניס את המספר
    if (NumInRow()==true) return false;
    if (NumInColumn()==true) return false;
    if(InSameSquer(WhatSquare())==true) return false;
    return true;
}// end U can put


public static boolean InSameSquer(int thesquare){// בודקת איזה ריבוע נמצא התא
    int i,j;

    if(thesquare==1){
            for (i=0;i<3;i++){
                for (j=0;j<3;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 1
    if(thesquare==2){
        for (i=0;i<3;i++){
            for (j=3;j<6;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 2
    if(thesquare==3){
        for (i=0;i<3;i++){
            for (j=6;j<9;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 3
    if(thesquare==4){
        for ( i=3;i<6;i++){
            for ( j=0;j<3;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 4
    if(thesquare==5){
        for ( i=3;i<6;i++){
            for (j=3;j<6;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 5
    if(thesquare==6){
        for (i=3;i<6;i++){
            for (j=6;j<9;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 6
    if(thesquare==7){
        for (i=6;i<9;i++){
            for (j=0;j<3;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 7
    if(thesquare==8){
        for ( i=6;i<9;i++){
            for ( j=3;j<6;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 8
    if(thesquare==9){
        for ( i=6;i<9;i++){
            for ( j=6;j<9;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 9
return false;
}



public static int WhatSquare(){// בודקת איזה ריבוע נמצא התא
    int Square=0;
    if(TestRow<3&&TestColumn<3) Square=1;
    if(TestRow<3&&TestColumn>2&&TestColumn<6) Square=2;
    if(TestRow<3&&TestColumn>5&&TestColumn<9) Square=3;
    if(TestRow>2&&TestRow<6&&TestColumn<3) Square=4;
    if(TestRow>2&&TestRow<6&&TestColumn>2&&TestColumn<6) Square=5;
    if(TestRow>2&&TestRow<6&&TestColumn>5&&TestColumn<9) Square=6;
    if(TestRow>5&&TestColumn<3) Square=7;
    if(TestRow>2&&TestColumn>2&&TestColumn<6) Square=8;
    if(TestRow>2&&TestColumn>5&&TestColumn<9) Square=9;

return Square;
}


public static boolean NumInRow(){
for (int i =0;i<9&&TestRow<9;i++){
    if(TheBoard[TestRow][i]==TheNumUPut)return true;}
return false;
}

public static boolean NumInColumn(){
for (int j =0;j<9&&TestColumn<9;j++){
    if(TheBoard[j][TestColumn]==TheNumUPut)return true;}
return false;
}

public static void PrintSudoku(){
    for (int i =0;i<9;i++){
        System.out.println();
            for (int j=0;j<9;j++){
                System.out.print(" "+ TheBoard[i][j]+" ");}
    }
    finish=true;
}

}//end all

3 个答案:

答案 0 :(得分:1)

很难说,但是您的示例包含递归方法,而大多数StackOverflowError都是由于处理不当而导致递归。

在这种情况下,finish(可能)总是false,因此SolveSudoku();一旦被调用足够时就会抛出StackOverFlowError

答案 1 :(得分:1)

你可能会导致无休止的递归 在开头添加打印输出声明(System.out.println) 每种方法,看看哪一个被称为很多次 这应该指出确切的问题。

答案 2 :(得分:1)

堆栈溢出:

When a program attempts to use more space than is available on the call stack 
(that is, when it attempts to access memory beyond the call stack's bounds, 
which is essentially a buffer overflow), the stack is said to overflow, typically
 resulting in a program crash.

您的递归可能太深,尝试用while / for循环替换它。 :)