Hard coding a two dimensional array error?
import java.util.Scanner;
public class SudokuPermuter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Sudoku Permuter.\n");
int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0开发者_Python百科, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
}
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 10; column++) {
System.out.print(sudoku[row] [column] + " ");
}
System.out.println();
}
}
}
I'm trying to enter a sudoku puzzle to be printed out, and it says there's a semicolon expected after the last line { 0, 9, 0, 7, 0, 1, 0, 4, 0 }
But when I put the semicolon in, it takes the bracket as ending the whole String args method. I don't know why.
You need the semicolon after the second closing bracket (to end the assignment statement):
int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
} ;
int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
};
No need for the constructor. It can be instantiated like this...
int[][] sudoku = {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
} ;
also your Array throwing Exception ArrayIndexOutOfBoundsException
you need to make your array bigger or change the loops to 9 inested of 10
I think you should altogether choose a more convenient way of representing your
Sudoku when filling a table by hand : something like an array of
string, that you split later, is for instance more convenient to fill-in.
After the (very simple) conversion, you have your array.
(Well i used Javascript notation here, conversion should not be hard.)
var oneSudokuStrings = [];
var sl=0;
oneSudokuStrings [sl++] = " O 8 0 4 0 2 0 6 0 ";
oneSudokuStrings [sl++] = " 0 3 4 0 0 0 9 1 0 ";
oneSudokuStrings [sl++] = " 9 6 0 0 0 0 0 8 4 ";
oneSudokuStrings [sl++] = " 0 0 0 2 1 6 0 0 0 ";
oneSudokuStrings [sl++] = " 2 0 0 0 0 9 6 0 0 ";
oneSudokuStrings [sl++] = " 0 1 0 3 5 7 0 0 8 ";
oneSudokuStrings [sl++] = " 8 4 0 0 0 0 0 7 5 ";
oneSudokuStrings [sl++] = " 0 2 6 0 0 0 1 3 0 ";
oneSudokuStrings [sl++] = " 0 9 0 7 0 1 0 4 0 ";
var oneSudoku = [];
for (var i=0; i<oneSudokuStrings.length; i++) {
oneSudoku[i]=oneSudokuStrings[i].match(/[^ ]+/g);
}
精彩评论