java - 数独 - java。我想看看我的行是否完整。不能有重复的数字

标签 java

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

这是我需要编码的内容,后面是它下面的代码:

 /**
 * 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
}

最佳答案

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

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

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

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

关于java - 数独 - java。我想看看我的行是否完整。不能有重复的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082366/

相关文章:

java - 如何达到其他级别深度的 json 值?

java - 我们如何在Java中调用多线程?

java - JSP 将单引号和双引号显示为符号

java - 如何在 Java 中执行 Windows 命令?

java - 如何在 FitNesse 测试用例中添加按钮?

java - 从 JSP 页面定向到 servlet 并返回到 JSP 会导致不同的 JSP 页面

java - 为什么私有(private)内部接口(interface)的方法必须是公共(public)的?

java - java.util.concurrent.Future 不一致?

java - tomcat 覆盖logging.properties 包含在共享库中

java - 有什么方法可以使用 Eclipse JDT 将测试代码转换为 MethodDeclaration 吗?