我一直在设置器上出现零点错误 我试图通过将数组的每个点设置为零来修复它,但它仍然无法正常工作。
请帮忙。
public class SudokuArray {
public int[][] sArray;
public int falsecheck = 0; //used as invalid/valid check
public SudokuArray()
{
int[][] sArray = new int[9][9];
for (int i=0; i<9; i++)
for (int x = 0; x<9; x++)
sArray[i][x]=0;
}
public int[][] getArray()
{
return sArray;
}//end array getter
public void setValue (int value, int column,int row)
{
sArray[column][row] = value;
}//ends setter
public void Check()
{
//row checking
for (int i = 0; i<9; i++)//decides which row
{
for (int x=0; x<9; x++)// decides first point
{
for (int y=0; y<9; y++) //decides second point in row
while (x != y && sArray[i][x] != 0 && sArray[i][y] != 0)// makes sure each point isn't zero (null)
{ // and making sure its not comparing a point to itself
if (sArray[i][x] == sArray[i][y])
falsecheck = 1;
}
}
}//ends row check
它切断了代码,所以这里是该课程的其余部分。
//column checking
for (int i = 0; i<9; i++)//decides which column
{
for (int x=0; x<9; x++)// decides first point
{
for (int y=0; y<9; y++) //decides second point in column
while (x != y && sArray[x][i] != 0 && sArray[y][i] != 0)
if (sArray[x][i] == sArray[y][i])
falsecheck = 1;
}
}//ends column check
//3x3 square check
for (int a = 1; a<4; a++)// check which column the square is in
{
for (int b = 1; b<4; b++) // check which row the square is in
for (int i = 3*a-3; i< 3*a; i++) //which row 1st point
for (int x = 3*b-3; x< 3*b; x++) //which column 1st point
{
for (int j = 3*a-3; j< 3*a; j++) //which row 1st point
for (int y = 3*b-3; y<3*b; y++)
while (sArray[i][x] !=0 && sArray[j][y] !=0)
while (j != i || y != x)
if (sArray[i][x] == sArray[j][y])
falsecheck = 1;
}
}//ends 3x3 square check
}//ends check
public int getCheck()
{
return falsecheck;
}// ends getter
}//ends class
import java.util.Random;
public class SudokuMain {
这是主要方法。
public static void main(String[] args)
{
SudokuArray Sudoku;
Sudoku = new SudokuArray();
Sudoku.getArray();
Random rand = new Random();
int column,row,value;
int Amount = rand.nextInt(15) + 15;
for ( int i= 0; i < Amount + 1; i++) // to assign each variable
{
column = rand.nextInt(9); // assigns random column
row = rand.nextInt(9); //assigns random row
do {
value = rand.nextInt(9); //assigns random value
Sudoku.setValue(value,column,row); //actually sets the value
Sudoku.Check();
} while (Sudoku.getCheck() == 1);
}
}
}
答案 0 :(得分:0)
首先,这是什么语言?我假设C#,但我不知道。我认为您可能遇到的问题是您使用的是锯齿状数组而不是多维数组。
int[][] sArray = new int[9][9];
不正确。你应该这样做:
int[,] sArray = new int[9,9];
或者这个:
int[][] sArray = new int[9][];
然后单独初始化每个子数组:
sArray[0] = new int[9];
sArray[1] = new int[1];
...
sArray[8] = new int[8];
你可以看到其他人给出了一个很好的解释here