简体   繁体   English

数独-Java。 我正在尝试查看我的行是否完整。 它不能重复数字

[英]Sudoku - java. I am trying to see if my row is complete or not. it cannot have a number repeated

I have been working on this code for a while and i am stuck on this code. 我已经在这段代码上工作了一段时间,而我一直坚持在这段代码上。 I am not sure what i am doing wrong. 我不确定自己在做什么错。 If someone could point me on the right direction 如果有人能指出我正确的方向

this is what i need to code followed by the code below it : 这是我需要编写代码,然后是下面的代码:

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

As noted in the other answer, you might have the indexing into your flag array wrong, because of java's zero-based indexing. 如另一个答案中所述,由于Java的从零开始的索引,您可能将对索引数组的索引设置为错误。

But there are other problems with the code as it is. 但是,代码本身仍然存在其他问题。

If the last row is valid, your method will return true based on its validity, regardless of having seen an invalid row earlier. 如果最后一行有效,则您的方法将基于其有效性返回true,而不管先前是否看到无效的行。

You would do better to set the result true before the loop, change it to false as appropriate, and never set it true again. 您最好在循环之前将结果设置为true,将其适当地更改为false, 再也不要再次将其设置为true。

Maybe you are going out of boundaries with the variable temp. 也许您在temp可变范围之外。 try setting 尝试设置

temp = game[i][j] - 1

Normally your numbers will be from 1 to 9 in game[][] so temp will also receive 1 to 9 and then you enter an array of size 9 to index 9, but you need to remember the count starts from zero so you actually want index 8 通常,您的数字在game [] []中将是1到9,因此temp也会收到1到9,然后您输入一个大小为9的数组到索引9,但是您需要记住计数从零开始,所以您实际上想要索引8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用和学习Java中的关键字“ this”。 在我的代码中我遇到了一个错误 - I am trying to use and learn the keyword “this” in java. In my code i got an error 我正在尝试使用Java更新oracle数据库中的密码。 难道我做错了什么? - I'm trying to update my password in oracle database using Java. Am I doing something wrong? 我正在尝试在Java中读取csv文件。 下面是我的代码 - I am trying to read a csv file in java. Below is my code 尝试使用Java查找GCD。 我在逻辑上坚持做某些事情 - Trying to find the GCD using java. I am stuck on some things with my do while logic 我试图在Java中创建一个数字模式。 第二个内部for循环的条件略有错误。 我无法纠正它 - i am trying to create a number pattern in java. There is a slight error in the condition of the second inner for loop. I am not able to rectify it 我正在尝试这个简单的数独 - I am trying this simple sudoku 我正在尝试在Java的多维数组中对角地向上搜索。 为什么我出现索引超出范围错误? - I am trying to search up to the right diagonally in my multidimensional array in java. Why am I getting index out of bounds error? 我有不同版本的javac和java。 我正在尝试在终端上运行.java文件。 但是它可以在日食中工作 - I have different versions of javac and java. I'm trying to run my .java file on terminal. But it works in eclipse 我想使用Java获取我的Android设备的Build Number,Android版本。 我使用以下代码: - I want to get Build Number, Android Version of my Android device using Java. I am using the following code: 试图用 Java 中的重复减法进行除法。 从这里去哪里? - Trying to divide by repeated subtraction in Java. Where to go from here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM