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 に答える