硬编码二维数组错误?

时间:2010-11-18 02:15:24

标签: java arrays

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();
        }
    }
}

我正在尝试输入要打印的数独谜题,并且它表示在最后一行{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }之后预计会有分号

但是当我输入分号时,它将括号作为整个String args方法的结束。我不知道为什么。

5 个答案:

答案 0 :(得分:3)

第二个右括号后面需要分号(结束赋值语句):

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 }
    } ;

答案 1 :(得分:1)

  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 }
    };

答案 2 :(得分:1)

不需要构造函数。它可以像这样实例化......

    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 }
} ;

答案 3 :(得分:0)

你的数组抛出异常ArrayIndexOutOfBoundsException

你需要让你的阵列更大或将循环更改为9 inested of 10

答案 4 :(得分:0)

我认为你应该选择一种更方便的方式代表你的 用手填充桌子时的数独:类似于数组的东西 稍后拆分的字符串,例如填写更方便 在(非常简单)转换之后,你有了你的数组。

(我在这里使用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);
}