gpt4 book ai didi

java - 使用 ArrayList 时出现明显错误,但遵循给定的语法(Stanford CS106A HangmanLexicon)

转载 作者:行者123 更新时间:2023-12-01 21:43:19 26 4
gpt4 key购买 nike

我即将完成 Hangman 作业(独立完成),最后一个任务是从 .txt 文件创建 ArrayList。我遵循给定的语法,但编译器不会接受两个错误:

/*
* File: HangmanLexicon.java
* -------------------------
* This file contains a stub implementation of the HangmanLexicon
* class that you will reimplement for Part III of the assignment.
*/

import acm.util.*;
import java.io.*;
import java.util.*;


public class HangmanLexicon {

//random number for getWord
private RandomGenerator rgen = RandomGenerator.getInstance();

//creates new arraylist
ArrayList<String> wordList = new ArrayList<String>();


//opens the text file
//Reads lines from the file into an ArrayList

try {
BufferedReader rd = new BufferedReader(new FileReader("HangmanLexicon.txt"));
while(rd.readLine() != null) {
String line = rd.readLine();
wordList.add(line);
}
} catch (IOException ex) {
System.out.println("bad/no file");
}


// Returns the number of words in the lexicon
public int getWordCount() {
int WordCount = wordList.size();
return WordCount;
}

/** Returns the word at the specified index. */

public String getWord(int index) {
int lexnum = rgen.nextInt(0, index);
String word = wordList.get(lexnum);
return word;
};

}

错误位于第 19 行和第 37 行:

19) Arraylist....错误:“{”应该在“;”之后

37) public int getWordCount() { .... 错误:“;”预计

这些问题有哪些修复方法?

最佳答案

try-catch block 需要位于方法或静态 block 内。

关于java - 使用 ArrayList 时出现明显错误,但遵循给定的语法(Stanford CS106A HangmanLexicon),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36219074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com