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)。我不确定将 Array List 的所有元素加在一起的部分。
这是怎么回事?提前致谢:)
最佳答案
我可以看到多个问题:
在类声明行中不应有分号。
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
变量,因此它可以被完全删除。
关于java - 需要帮助计算 arrayList 中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10173554/