java 硬编码二维数组错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4211054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hard coding a two dimensional array error?
提问by anonymous
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, 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 }
我正在尝试输入要打印的数独谜题,它说在最后一行之后应该有一个分号 { 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.
但是当我把分号放进去时,它把括号作为整个 String args 方法的结尾。我不知道为什么。
回答by Thilo
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 }
} ;
回答by Adam
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 }
} ;
回答by Calvin Allen
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 }
};
回答by Ahmed Al-Obaidi
also your Array throwing Exception ArrayIndexOutOfBoundsException
还有你的数组抛出异常 ArrayIndexOutOfBoundsException
you need to make your array bigger or change the loops to 9 inested of 10
你需要让你的数组更大或将循环更改为 9 inested of 10
回答by GameAlchemist
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.)
(好吧,我在这里使用了 Javascript 符号,转换应该不难。)
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);
}