2012-04-16 82 views
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)设置。我不确定将数组列表的所有元素相加的部分。需要帮助计算阵列中的字符列表

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

+2

编译器给出了什么错误? – luketorjussen 2012-04-16 11:55:47

回答

1

在你createPuzzleGrid,有两大}在for循环

的correctec版本:

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

删除分号这里.... public class WordSearchPuzzle;

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

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

+0

非常感谢! – User123Rocks 2012-04-16 13:21:36

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变量,因此可以将其完全删除。