-3

在这个项目中,我正在尝试编写一个程序来创建一个 9x9 数独板,其中包含 9 个 3x3 子网格,以及列出字母 a 到 i 的标题行和列。该程序编译正确,但是当我点击运行时,它给出了以下错误:

java.lang.ArrayIndexOutOfBoundsException: 0
at Sudoku.main(Sudoku.java:218)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at `enter code here`edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

现在,当我提交此文件时,评分程序指出我print()的 、rowsComplete()columnsComplete()isComplete()方法不正确,并且我main()正在抛出java.util.NoSuchElementException. 我很困惑为什么会这样。这是我的 Java 代码,以及关于这些方法应该做什么的注释。

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Sudoku 
{
public static final int SIZE = 9;
public static final int SUBGRID = 3;
public int[][] game;   
public int[][] originalGame; 
public Sudoku(String puzzleFile)
{
    try
    {
        game = new int[SIZE][SIZE];
        originalGame = new int[SIZE][SIZE];
        File file = new File(puzzleFile);
        Scanner in = new Scanner(file);
        int i = 0;
        int j = 0;
        int k;
        for (i = 0; i<SIZE; i++){
            for (j = 0; j<SIZE; j++){
                k = in.nextInt();
                game[i][j]=k;
                originalGame[i][j] = k;
            }

        }
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFound: " + e.getMessage());
    }   
}

public void setZero(int[] array)
{
    int i = 0;
    for (i = 0; i < array.length; i++)
    {
        array[i] = 0;  
    }
}
/**
 * This method determines whether the ints 1-9 are present exactly
 * once in each row.  Sets valSeen[i] = 1 if it sees i.  If at any
 * point valSeen[i] is already 1, the rows are not complete because of 
 * duplicate entries.  
 * 
 * If game[x][y] == -1, there is a blank entry so the row cannot be complete.
 * 
 * @param valSeen: an array of ints that serve as flags to indicate whether
 *                 their entry has been seen before or not.
 * 
 * returns: true if each digit 1-9 is present in the row exactly once, else false
 **/
public boolean rowsComplete(int[] valSeen)
{     
    int temp;
    int k = 0;
    for(int rows = 0; rows<SIZE; rows++){
        for(int cols = 0; cols<SIZE; cols++){
            if(game[rows][cols]==-1)
                return false;
            temp = game[rows][cols];
            valSeen[temp-1]++;
        }
        for(k=0; k<valSeen.length; k++){
            if(valSeen[k]!=1)
                return false;
            else return true;
        }
        setZero(valSeen);
    }
    return true;
} 
/**
 * This method determines whether the ints 1-9 are present exactly
 * once in each column.  Sets valSeen[i] = 1 if it sees i.  If at any
 * point valSeen[i] is already 1, the rows are not complete because of 
 * duplicate entries.  
 * 
 * If game[x][y] == -1, there is a blank entry so the row cannot be complete.
 * 
 * @param valSeen: an array of ints that serve as flags to indicate whether
 *                 their entry has been seen before or not.
 * 
 * returns: true if each digit 1-9 is present in the column exactly once, else false
 **/
public boolean columnsComplete(int[] valSeen)
{
    int temp;
    int k = 0;
    for(int cols = 0; cols<SIZE; cols++){
        for(int rows = 0; rows<SIZE; rows++){
            if(game[rows][cols]==-1)
                return false;
            temp = game[rows][cols];
            valSeen[temp-1]++;
        }
        for(k=0; k<valSeen.length; k++){
            if(valSeen[k]!=1)
                return false;
            else return true;
        }
        setZero(valSeen);
    }
    return true;
}
/**
 * This method determines whether the ints 1-9 are present exactly
 * once in each subgrid.  Sets valSeen[i] = 1 if it sees i.  If at any
 * point valSeen[i] is already 1, the rows are not complete because of 
 * duplicate entries.  
 * 
 * If game[x][y] == -1, there is a blank entry so the row cannot be complete.
 * 
 * @param valSeen: an array of ints that serve as flags to indicate whether
 *                 their entry has been seen before or not.
 * 
 * returns: true if each digit 1-9 is present in each subgrid exactly once, else false
 **/   
public boolean subgridsComplete(int[] valSeen)
{
    int temp;
    for(int rows=0; rows<SIZE; rows+=3){
        for (int cols=0; cols<SIZE; cols+=3){
            for(int subrows=0; subrows<SUBGRID; subrows++){
                for (int subcols=0; subcols<SUBGRID; subcols++){
                    temp= game[rows+subrows][cols+subcols];
                    if(temp==-1)
                        return false;
                    else
                        valSeen[temp-1]++;
                }
            }
            for(int k=0; k<valSeen.length; k++){
                if(valSeen[k]!=1)
                    return false;
                else return true;
            }
            setZero(valSeen);
        }
    }
    return true;
}
// Create the array valSeen here. I suggest making it = new int[SIZE+1].
// That way, it will have indexes 0-9, so the ints 1-9 can go into indexes
// 1-9 instead of mapping them to 0-8 by subtracting 1.

// Call rowsComplete(), columnsComplete(), and subgridsComplete().
// Be SURE to initialize valSeen to 0 before each method call by using setZero().
public boolean isComplete()
{
    int [] valSeen = new int[SIZE+1];
    setZero(valSeen);
    if(rowsComplete(valSeen) && columnsComplete(valSeen) && subgridsComplete(valSeen))
        return true;
    else
        return false;
}

public String makeHeader()
{
    String header = "   ";
    for (int x = 97; x<106; x++)
        header += ((char)x) + " | " + " ";
    return header;
}
 /**
 * Prints out the grid.  Each entry has a space to either side, columns are separated by '|'
 * within the grid / between the header and the grid but not externally.  See the specification 
 * for a detailed example.  -1 is replaced with '_'.
 * 
 * Remember that 'a' + 1 = 'b'
 * 
 * Prints the grid to standard out.
 **/
public void print()
{
    System.out.println(makeHeader());
    for(int rows=0; rows<SIZE; rows++){
        System.out.print(" "+(char)('a'+rows));
        for (int cols=0; cols<SIZE; cols++){
            if (game[rows][cols]==-1)
                System.out.print(" | _");
            else
                System.out.print(" | "+game[rows][cols]);
        }
        System.out.println();
    }
}
public void move(String row, String col, int val)
{
    int rowNumber = ((int)(row.charAt(0)-97));
    int columnNumber = ((int)(col.charAt(0)-97));
    if(originalGame[rowNumber][columnNumber]==-1)
        game[rowNumber][columnNumber]=val;
}

public static void main(String[] args)
{
    Sudoku puzzle = new Sudoku(args[0]);
    Scanner s = new Scanner(System.in);
    System.out.println("");
    boolean gameplay = true;
    while (gameplay){
        puzzle.print();
        if(puzzle.isComplete()){
            System.out.println("Puzzle Complete!");
            gameplay=false;
        } else {
            System.out.println("Puzzle Incomplete!");
            System.out.println("Enter new value <row col val> :");
            String rowv = s.next();
            String colv = s.next();
            int valv = s.nextInt();
            puzzle.move(rowv, colv, valv);
        }
    }
}
}
4

1 回答 1

0

main方法上,

Sudoku puzzle = new Sudoku(args[0]);

您的程序需要一个参数来初始化,该参数Sudoku取自用户。

String[] argsinmain是程序的参数数组,在启动程序时作为参数给出。

对于你的程序,你必须开始你的Sudoku.classas

java Sudoku argument

你必须用参数运行你的程序,否则你会得到java.lang.ArrayIndexOutOfBoundsException: 0

于 2013-10-01T02:00:46.747 回答