我正在研究一个检查数独解决方案有效性的程序。我接近完成它,但出于某种原因,当我尝试调用我创建的某些方法时,编译器返回它们是未定义的。
主要有三个类:
一个sudoku类,它实现了可迭代的接口。它包含一个二维数组,这是拼图。构造函数从扫描仪输入中获取文件并构建拼图。它有一个迭代器方法来满足接口要求。此方法返回类型为SudokuIterator的交互器。
一个SudokuIterator类,它实现了Iterator接口。这是Sudoku类的私有内部类。它还有一个二维数组和一个光标作为属性。它具有标准的hasNext(),next()和remove()存根以满足接口。我还添加了一个nextColumn()和nextBox(),它根据光标位置返回一个数组。已覆盖下一个方法以返回行。
最后是Validator。这种方法是主要方法。它有一个方法isSolution(),它返回一个布尔值,具体取决于对从SudokuIterator类中定义的方法返回的每个数组的分析。
这就是我出现问题的地方;当使用iterator方法实例化并返回一个SudokuIterator并尝试然后使用我添加的nextColumn()和nextBox()方法时,编译器返回那些方法未定义为Iterator。
非常感谢任何建议或意见。提前谢谢。
import java.util.*;
/**
* Sudoku class represents the matrix of cells in a Sudoku puzzle
* @version 01/05/2012
* @author Bob Wilson
*/
public class Sudoku implements Iterable<Cell []>
{
private Cell [] [] puzzle;
/**
* Default constructor should not be called. Make it private.
*/
private Sudoku() {}
/**
* Puzzle constructor that uses a Scanner object to read a file.
* File contains 81 numbers that are the values of the 81 cells.
* @param file a Scanner object on a File object
*/
public Sudoku(Scanner file)
{
int size = file.nextInt();
System.out.println("Size: " + size);
puzzle = new Cell[size][size];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
puzzle[i][j] = new Cell(file.nextInt());
}
public int getLength(){
return this.puzzle.length;
}
class SudokuIterator implements Iterator<Cell []> {
private Cell [][] puzzle;
private int cursor;
public SudokuIterator(Cell [][] puzzle){
this.puzzle = puzzle;
cursor = 1;
}
public boolean hasNext(){
if(cursor <= this.puzzle.length){
return true;
}
return false;
}
public Cell[] next(){
Cell[] row = puzzle[cursor-1];
cursor++;
return row;
}
public Cell[] nextColumn(){
Cell[] column = new Cell[puzzle.length];
for(int i = 0; i < puzzle.length; i++){
column[i] = puzzle[i][cursor-1];
}
cursor++;
return column;
}
public Cell[] nextBox(){
Cell[] box = new Cell[puzzle.length];
int boxIndex = 0;
for(int i = ((cursor - 1)/((int)Math.sqrt(puzzle.length)))*(int)Math.sqrt(puzzle.length) ; i < ((cursor - 1)/((int)Math.sqrt(puzzle.length)))*(int)Math.sqrt(puzzle.length) + (int)Math.sqrt(puzzle.length); i++){
for(int j = (((cursor - 1) + ((int)Math.sqrt(puzzle.length))) % ((int)Math.sqrt(puzzle.length))) * ((int)Math.sqrt(puzzle.length)); j < (((cursor - 1) + ((int)Math.sqrt(puzzle.length))) % ((int)Math.sqrt(puzzle.length))) * ((int)Math.sqrt(puzzle.length)) + ((int)Math.sqrt(puzzle.length)); j++){
box[boxIndex] = puzzle[i][j];
}
}
cursor++;
return box;
}
public void remove(){}
}
/**
* Generates and returns a String representation of the puzzle cells
* @return A String representing the contents of the puzzle array
*/
public String toString()
{
// display the puzzle
String value = "Puzzle is:\n";
for (int i = 0; i < puzzle.length; i++) {
for (int j = 0; j < puzzle[i].length; j++)
value += puzzle[i][j].toString();
value += "\n";
}
return value;
}
/**
* Instantiates and returns a new SudokuIterator object
* @return A SudokuIterator object on the puzzle array
*/
public SudokuIterator iterator(){
SudokuIterator iterator = new SudokuIterator(this.puzzle);
return iterator;
}
}
/* 201340 */
import java.util.*;
import java.io.*;
/**
* This class instantiates a Sudoku object passing a Scanner on a
* file to the Sudoku constructor. It prints the puzzle using the
* Sudoku toString method. It determines if the digit matrix is a
* valid solution for a Sudoku puzzle or not and prints the result.
*
* @version 01/05/2012
* @author Bob Wilson
*
*/
public class SudokuValidator
{
private Sudoku puzzle;
/**
* @param args - not used
*/
public static void main( String [] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter name of file containing puzzle to verify");
SudokuValidator myValidator = new SudokuValidator(scan.nextLine());
System.out.println(myValidator.isSolution());
}
public SudokuValidator(String fileName)
{
Scanner file = null;
try
{
file = new Scanner(new File(fileName));
}
catch (Exception e)
{
System.out.println("Bad file name");
System.exit(0);
}
puzzle = new Sudoku(file);
System.out.println(puzzle);
}
public boolean isSolution(){
boolean flag = true;
Iterator<Cell[]> game = puzzle.iterator();
while(game.hasNext()){
Cell[] row = game.next();
Cell[] column = game.nextColumn();
Cell[] box = game.nextBox();
for(Cell i: row){
for(Cell j: row){
if(j.equals(i.getValue())){
flag = false;
return flag;
}
}
}
}
return flag;
}
} /* 201340 */
答案 0 :(得分:3)
问题是,game
引用变量的声明类型是Iterator
,它没有定义任何nextColumn()
方法,因此编译器无法找到它。您可以通过将声明的类型更改为Sudoku.SudokuIterator
来解决此问题(因为这是一个内部类)。
变化:
Iterator<Cell[]> game = puzzle.iterator();
为:
Sudoku.SudokuIterator game = puzzle.iterator();