我在此程序上打印生成的数独时遇到问题。每次我尝试打印它时,它都会显示“null”。原来代码没有main方法所以加了一个。
class GenerateSudoku{
public static int[][][] sudoku;
public static Random rand=new Random();
GenerateSudoku(int[][][] sudoku){
this.sudoku=sudoku;
for(int ctr=0; ctr<sudoku.length; ctr++){
for(int ct=ctr; ct<sudoku.length; ct++){
double first=rand.nextDouble(), second=rand.nextDouble();
if(first>1-second){
this.sudoku[ct][ctr][0]=0;
this.sudoku[ct][ctr][1]=1;
}
else{
this.sudoku[ct][ctr][1]=0;
}
if(ct!=ctr && first>1-second){
this.sudoku[ctr][ct][0]=0;
this.sudoku[ctr][ct][1]=1;
}
else if(ct!=ctr){
this.sudoku[ctr][ct][1]=0;
}
}
}
}
public int[][][] getSudoku(){
return sudoku;
}
private void sop(Object obj){
System.out.println(obj+"");
}
public static void main(String[] args) {
System.out.println(sudoku);
}
}
答案 0 :(得分:1)
如评论中所述,您应该创建 IProgress<T>
类的实例并从 GenerateSudoku
方法获取结果数独:
getSudoku()
生成:
import java.util.Random;
class GenerateSudoku{
public static int[][][] sudoku;
public static Random rand=new Random();
GenerateSudoku(int[][][] sudoku){
this.sudoku=sudoku;
for(int ctr=0; ctr<sudoku.length; ctr++){
for(int ct=ctr; ct<sudoku.length; ct++){
double first=rand.nextDouble(), second=rand.nextDouble();
if(first>1-second){
this.sudoku[ct][ctr][0]=0;
this.sudoku[ct][ctr][1]=1;
}
else{
this.sudoku[ct][ctr][1]=0;
}
if(ct!=ctr && first>1-second){
this.sudoku[ctr][ct][0]=0;
this.sudoku[ctr][ct][1]=1;
}
else if(ct!=ctr){
this.sudoku[ctr][ct][1]=0;
}
}
}
}
public int[][][] getSudoku(){
return sudoku;
}
private void sop(Object obj){
System.out.println(obj+"");
}
public static void main(String[] args) {
int[][][] sudoku = new int[9][9][2];
GenerateSudoku generator = new GenerateSudoku(sudoku);
sudoku = generator.getSudoku();
for(int i = 0 ; i < sudoku.length; i++) {
for(int j = 0 ; j < sudoku.length; j++) {
System.out.print(sudoku[i][j][1]);
}
System.out.println();
}
}
}