1
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).length ; i++){
          letterCount = puzzleWords + letterCount ;
            }
        }
        gridDimensions = letterCount * 1.5;
        puzzle[gridDimensions][gridDimensions];
    }

    public WordSearchPuzzle(String wordFile, int wordCount,
    int shortest, int longest)
    {
        // puzzle generation using words from a file
        // The user supplies the filename. In the file 
        // the words should appear one per line.
        // The wordCount specifies the number of words
        // to (randomly) select from the file for use in
        // the puzzle.
        // shortest and longest specify the shortest
        // word length to be used and longest specifies
        // the longest word length to be used.
        // SO, using the words in the file randomly select
        // wordCount words with lengths between shortest
        // and longest.

    }

    private ArrayList<String> loadWordsFromFile(String filename, int shortest, int longest)
    {
        // BasicEnglish.txt - the 850 words of Basic English
        // BNCwords.txt - "the 6,318 words with more than 800 occurrences in
        //the whole 100M-word BNC"
        try {
            FileReader aFileReader = new FileReader(filename);
            BufferedReader aBufferReader = new BufferedReader(aFileReader);
            String lineFromFile;
            int len ;
            ArrayList<String> words = new ArrayList<String>();
            lineFromFile = aBufferReader.readLine() ;
            while (lineFromFile != null) {  
                len = lineFromFile.length() ;
                if(len >= shortest && len <= longest) {
                    words.add(lineFromFile.toUpperCase());
                }
                lineFromFile = aBufferReader.readLine() ;
            }
            aBufferReader.close();
            aFileReader.close();
            return words ;
        }
        catch(IOException x)
        {
            return null ;
        }
    }

    // The dimensions of the puzzle grid should be set
    // by summing the lengths of the words being used in the
    // puzzle and multiplying the sum by 1.5 or 1.75
    // or some other (appropriate) scaling factor to
    // ensure that the grid will have enough additional
    // characters to obscure the puzzle words. Once
    // you have calculated how many characters you are
    // going to have in the grid you can calculate the
    // grid dimensions by getting the square root (rounded up)
    // of the character total.
}

嗨,我上大学时必须在这里做的小型 Java 项目。这是我到目前为止所拥有的。我不明白为什么它不编译。我编写了用于生成网格的代码;网格尺寸由输入词设置(所有输入词的字母总和 * 1.5)。我不确定将数组列表的所有元素加在一起的部分。

这是怎么回事?提前致谢 :)

4

3 回答 3

2

我可以看到多个问题:

在类声明行中不应有分号。

public class WordSearchPuzzle

正如 Nettogrof 所示,您的方法中有太多 } createPuzzleGrid

其中的循环createPuzzleGrid使用不存在的方法。没有size为数组列表提供参数的方法。它也没有找到那个点的字符串长度你的循环createPuzzleGrid应该是:

for (int i = 0; i < puzzleWords.size; i++) {
    String item = puzzleWords.get(i);
    int itemLength = item.length();
    letterCount = letterCount + itemLength;
}

作为额外说明,该方法的最后一行访问puzzle数组但不执行任何操作,因此可以删除此行。事实上,没有任何方法使用该puzzle变量,因此可以将其完全删除。

于 2012-04-16T12:05:29.673 回答
2

去掉这里的分号.... public class WordSearchPuzzle;

这不是一个声明。 puzzle[gridDimensions][gridDimensions];

puzzleWords.size(i).length在 for 循环中给出了问题。如果您想要列表中的元素数量,puzzleWords.size()它将起作用。然后 letterCount = puzzleWords + letterCount ; ,你有不兼容的类型,ArrayList + int,你是要使用puzzleWords.size()而不是puzzleWords吗?

于 2012-04-16T11:58:36.277 回答
1

在您的 createPuzzleGrid 中,您的 For 循环中有两个 }

正确的版本:

private void createPuzzleGrid()
{
  int i;
    for(i = 0; i < puzzleWords.size(i).length ; i++){
      letterCount = puzzleWords + letterCount ;
    }
    gridDimensions = letterCount * 1.5;
    puzzle[gridDimensions][gridDimensions];
}
于 2012-04-16T11:55:56.327 回答