-1

嗨,我是 Java 新手,在创建我的单词搜索拼图程序时遇到了困难。我正在尝试让用户输入单词的 ArrayList,以便可以将其输入到我的难题中,但是当我输入字符串数组时,我一直收到错误,有人可以帮忙吗?

 import java.io.* ;
import java.util.ArrayList ;
public class WordSearchPuzzle
{
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;
    private int letterCount = 0 ;
    private int gridDimensions;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    private void createPuzzleGrid()
    {
        int i;
        for(i = 0; i < puzzleWords.size() ; i++){
            letterCount = puzzleWords.size() + letterCount ;
        }
        gridDimensions = letterCount * 2;
        puzzle = new char[gridDimensions][gridDimensions] ;
    }

    private void generateWordSearchPuzzle()
    {

    }

    public void fillPuzzle()
    {
        int i, j, r;
        for (i = 0; i < this.gridDimensions; i++)
            for (j = 0; j < this.gridDimensions; j++)
                if (puzzle[i][j] == ' ') {
                    r = (int) (Math.random() * letterCount);
                    puzzle[i][j] = letterCount.charAt(r);
        }
    }

    public void fill() 
    {
        int i, j, gridDimensions, r, c;
        boolean added;
        this.clear();
        for (i = 0; i < WordSearch.words.size(); i++) {
            ws = (WSWord) WordSearch.words.elementAt(i);
            row = (int) (Math.random() * this.rows);
            col = (int) (Math.random() * this.cols);
            added = false;
        }
4

3 回答 3

0

If you're referring to puzzle as your array of Strings, it isn't, it's a two-dimensional array of char. Perhaps you need

private String puzzle[];

Unfortunately it's not very clear from the given code example what exactly are you having an issue with.

于 2012-04-10T10:46:44.137 回答
0

You can't just use the = to copy arraylist and its contents: From a similar question: clone(): ArrayList.clone() I thought does a shallow copy

Adapted to your case:

this.puzzleWords = new ArrayList(userSpecifiedWords)
于 2012-04-10T10:47:43.557 回答
0

Initialize puzzleWords.

private ArrayList<String> puzzleWords = new ArrayList<String> ();
于 2012-04-10T10:47:45.883 回答