我正在研究一個檢查數獨解決方案的有效性的程序。我已經接近完成了,但由於某種原因,當我嘗試調用某些已創建的方法時,編譯器會返回它們未定義的方法。泛型和在實現Iterable接口的類中使用新創建的方法
有三種主要類型:
數獨類,它實現了可迭代接口。它包含一個二維數組,就是難題。構造函數從掃描儀輸入中獲取文件並構建難題。它有一個迭代器方法來滿足接口要求。此方法返回類型爲SudokuIterator的interator。
SudokuIterator類,它實現了Iterator接口。這是Sudoku類的私人內部類。它也有一個2維數組和一個光標作爲屬性。它有標準的hasNext(),next()和一個remove()存根來滿足接口。我還添加了一個nextColumn()和nextBox(),它根據光標位置返回一個數組。下一個方法已被覆蓋返回行。
最後是Validator。這種方法是主要的方法。它有一個方法isSolution(),它返回一個布爾值,取決於從SudokuIterator類中定義的方法返回的每個數組的分析。
這是我的問題出現的地方;當使用迭代器方法實例化並返回一個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 */
現在我得到一個編譯錯誤說: 「SudokuIterator不能被解析爲一個類型。」 – Atache
@Atache。添加一個導入到你的班級。 –
@Atache。如果你有你的類在默認包,這是非常糟糕的,然後更改引用類型爲'Sudoku.SudokuIterator' –