-1

我在编程方面还很陌生,并且一直在通过 Daniel Y. Liang 的 Java 简介文本自学。我已经完成了第 7 章(一维数组)并且一直在进行以下练习:找到 8 个皇后谜题的解决方案。

我还没有学习递归、多维数组、对象或类,所以我想我应该用我所知道的(循环、if-else 语句、一维数组……)来实现程序。我的问题是以下代码似乎陷入了无限循环,我花了几个小时尝试调试它。如果有人能暗示我应该看哪里,那会很有帮助。提前致谢。

package myfirstproject;
/*The 8 Queens puzzle is used to determine the position of
8 queens on a chess board such that no one queen can attack
another. There are many possible solutions.*/
import java.util.Arrays;
public class practice1{
    public static void main(String [] args){
//initialize an array of size 64
boolean[] columns = new boolean[64];
/*
 * i % 7 is the row. i / 8 is the column.
 * First we will randomly initialize the position of Q in column 1
 * by setting all other elements in the column, row, and diagonals to true.
 */
int randInt= (int)Math.random()*7;
int rowsBelow = 7 - randInt;
int rowsAbove = randInt;
int columnsRight = 7;
int columnsLeft = 0;

//set up elements true.
    for (int j = 1; j <= rowsAbove; j++){
        columns[randInt - j] = true;
    }
//set down elements true.
    for (int j = 1; j <= rowsBelow; j++){
        columns[randInt + j] = true;
}
//set right elements true.
for (int j = 1; j <= columnsRight; j++){
        columns[randInt + (j)*8] = true;
    }
//set right-down diagonal elements to true.
for (int j = 1; ; ){
    int thisRowBelow = rowsBelow;
    int thisColumnRight = columnsRight;
    if((thisRowBelow != 0)||(thisColumnRight != 0)){
        int temp = randInt;
        columns[temp + 8 + j] = true;
        temp = temp + 8 + j;
        thisRowBelow = (temp % 7);
        thisColumnRight = 7 - (temp / 8);
    }
    else break;
}
//set right-up diagonal elements to true.
for (int j = 1; ;){
    int thisRowAbove = rowsAbove;
    int thisColumnRight = columnsRight;
    if ((thisRowAbove != 0)||(thisColumnRight != 0)){
        int temp = randInt;
        columns[temp + 8 - j] = true;
        temp = temp + 8 - j;
        thisRowAbove = 7 - (temp % 7);
        thisColumnRight = 7 - (temp / 8);
    }
    else break;
}
//find a general solution for all other columns
for (int i = 8; i < 64; i++){
    //run through and select the first false element in each column
    if (columns[i] == false){
        columnsRight = 7 - (i / 8);
        columnsLeft = (i / 8);
        rowsAbove = 7 - (i % 7);
        rowsBelow = (i % 7);
        //set right elements to true
        for (int j = 1; j <= columnsRight; j++){
            columns[i + (j)*8] = true;
        }
        //set left elements to true
        for (int j = 1; j <= columnsLeft; j++){
            columns[i - (j)*8] = true;
        }
        //set up elements to true
        for (int j = 1; j <= rowsAbove; j++){
            columns[i -j] = true;
        }
        //set down elements to true
        for (int j = 1; j <= rowsBelow; j++){
            columns[i + j] = true;
        }
        //set right-up elements true
        for (int j = 1; ;){
            int thisRowAbove = rowsAbove;
            int thisColumnRight = columnsRight;
            if ((thisRowAbove != 0)||(thisColumnRight != 0)){
                int temp = i;
                columns[temp + 8 - j] = true;
                temp = temp + 8 - j;
                thisRowAbove = 7 - (temp % 7);
                thisColumnRight = 7 - (temp / 7);
            }
            else break;
        }
        //set right-down elements true
        for (int j = 1; ; ){
            int thisRowBelow = rowsBelow;
            int thisColumnRight = columnsRight;
            if((thisRowBelow != 0)||(thisColumnRight != 0)){
                int temp = i;
                columns[temp + 8 + j] = true;
                temp = temp + 8 + j;
                thisRowBelow = (temp % 7);
                thisColumnRight = (7 - (temp / 8));
            }
            else break;
        }
        //set left-down elements true
        for (int j = 1; ;){
            int thisRowBelow = rowsBelow;
            int thisColumnLeft = columnsLeft;
            if ((thisRowBelow != 0)||(thisColumnLeft != 0)){
                int temp = i;
                columns[temp - 8 + j] = true;
                temp = temp - 8 + j;
                thisRowBelow = (temp % 7);
                thisColumnLeft = (temp / 8);
            }
            else break;
        }
        //set left-up elements true
        for (int j = 1; ;){
            int thisRowAbove = rowsAbove;
            int thisColumnLeft = columnsLeft;
            if ((thisRowAbove !=0)||(thisColumnLeft != 0)){
                int temp = i;
                columns[temp - 8 - j] = true;
                temp = temp - 8 - j;
                thisRowAbove = 7 - (temp % 7);
                thisColumnLeft = (temp / 8);
            }
        }
    }
}
//print array elements that are false.
for (int i = 0; i < 64; i++){
    if (i % 7 == 0){
        if (columns[i] == false)
            System.out.println("|Q");
        else
            System.out.println("| ");
    }
    else
        if (columns[i] == false)
            System.out.print("|Q");
        else
            System.out.print("| ");

}
}
}
4

2 回答 2

1

您的代码进入这个无限循环:

    for (int j = 1;;) {
        int thisRowBelow = rowsBelow;
        int thisColumnRight = columnsRight;
        if ((thisRowBelow != 0) || (thisColumnRight != 0)) {
            int temp = randInt;
            columns[temp + 8 + j] = true;
            temp = temp + 8 + j;
            thisRowBelow = (temp % 7);
            thisColumnRight = 7 - (temp / 8);
        } else {
            break;
        }
    }

您在 if 语句中更改了 thisRowBelow 和 thisColumnRight 的值,但是因为每次您的代码返回 for 语句时,您将它们重置为 columnRight 和 rowsBelow 的值,您的代码将永远不会遇到 break 语句。

事实上,如果它命中 if 语句一次,它将永远如此。

一个小技巧:尝试在方法中组织你的代码。

于 2014-02-23T22:21:08.333 回答
-1

你是说这一行吗:

int randInt= (int)Math.random()*7;

实际上是:

int randInt= ( new Random() ).nextInt() % 7;
于 2014-02-23T22:15:20.337 回答