0

我已经在这段代码上工作了一段时间,我被困在这段代码上。我不确定我做错了什么。如果有人能指出我正确的方向

这就是我需要编写的代码,然后是下面的代码:

 /**
 * This method determines whether the ints 1-9 are present exactly
 * once in each column.  Sets valSeen[i] = 1 if it sees i.  If at any
 * point valSeen[i] is already 1, the rows are not complete because of 
 * duplicate entries.  
 * 
 * If game[x][y] == -1, there is a blank entry so the row cannot be complete.
 * 
 * @param valSeen: an array of ints that serve as flags to indicate whether
 *                 their entry has been seen before or not.
 * 
 * returns: true if each digit 1-9 is present in the column exactly once, else false
 **/

public boolean rowsComplete(int[] valSeen)
    {   
    // Write the appropriate nested loops to check the rows.
    int temp = 0;
    boolean val = false;         
    for(int i = 0; i < SIZE; i++) {        //row [i]
        for(int j = 0; j < SIZE; j++) {    //columns [j]

            temp = game[i][j];

            if( temp != -1) {                //make sure the index of the row is not empty

                if(valSeen[temp] != 1) {     //make sure the number was not previously                        

                    valSeen[temp] = 1;
                    val = true;
                }

                else {
                   val = false;
                }

            }

            else
              val = false;

        }

        setZero(valSeen);     //sets all the indexes of valseen to zero aFter each row

    }


    // Remember to reset valSeen to 0 after each row.

    return val; // Change this, placeholder so your code will compile
}
4

2 回答 2

2

如另一个答案中所述,由于java的从零开始的索引,您可能对标志数组的索引错误。

但是代码还存在其他问题。

如果最后一行有效,则您的方法将根据其有效性返回 true,而不管之前是否看到过无效行。

您最好在循环之前将结果设置为 true,根据需要将其更改为 false,并且永远不要再次将其设置为 true。

于 2013-09-29T20:37:00.857 回答
1

也许您将超出变量 temp 的界限。尝试设置

temp = game[i][j] - 1

通常你的数字会在 game[][] 中从 1 到 9,所以 temp 也会收到 1 到 9,然后你输入一个大小为 9 的数组来索引 9,但你需要记住计数从零开始,所以你实际上想要索引 8

于 2013-09-29T19:53:45.097 回答