“ 背单词 ”:界面可以再好看一点;可以去掉音标;可以改变单词显示的速度;可以增加标记生词并记到生词本中;可以增加测试的功能(单词含义可以随机选4个词的含义来让用户选择)等等。附件中有两个代码,一个单词本。参考代码见附件。
时间: 2024-02-15 22:05:20 浏览: 63
好的,根据您的要求,以下是一个简单的Java背单词程序的改进示例:
```java
import java.util.*;
public class WordFlashcards {
private static final int WORD_DISPLAY_SPEED = 1000; // 单词显示速度,单位为毫秒
private static final int TEST_QUESTION_NUMBER = 4; // 测试题目数
private Map<String, String> words; // 单词的键值对集合
private Set<String> unknownWords; // 生词集合
private Scanner scanner;
public WordFlashcards() {
words = new HashMap<>();
unknownWords = new HashSet<>();
scanner = new Scanner(System.in);
}
public void addWord(String word, String meaning) {
words.put(word, meaning);
}
public void removeWord(String word) {
words.remove(word);
}
public void displayWords() {
List<String> keys = new ArrayList<>(words.keySet());
Collections.shuffle(keys);
for (String key : keys) {
System.out.println("单词:" + key);
System.out.print("中文释义:");
String meaning = words.get(key);
for (int i = 0; i < meaning.length(); i++) { // 控制单词显示速度
System.out.print(meaning.charAt(i));
try {
Thread.sleep(WORD_DISPLAY_SPEED);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println();
System.out.print("是否认识这个单词?(y/n)");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("n")) { // 标记生词并加入生词本
unknownWords.add(key);
}
}
}
public void test() {
List<String> keys = new ArrayList<>(words.keySet());
Collections.shuffle(keys);
int correct = 0;
for (String key : keys) {
String[] options = new String[TEST_QUESTION_NUMBER];
options[0] = words.get(key); // 正确的答案
for (int i = 1; i < TEST_QUESTION_NUMBER; i++) { // 随机选取其他三个错误答案
String option;
do {
int index = new Random().nextInt(keys.size());
option = words.get(keys.get(index));
} while (option.equals(options[0]));
options[i] = option;
}
Collections.shuffle(Arrays.asList(options));
System.out.println("单词:" + key);
for (int i = 0; i < TEST_QUESTION_NUMBER; i++) {
System.out.println((i + 1) + ". " + options[i]);
}
System.out.print("请选择单词的含义(1-" + TEST_QUESTION_NUMBER + "):");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
if (options[choice - 1].equals(words.get(key))) {
System.out.println("回答正确!");
correct++;
} else {
System.out.println("回答错误,正确答案为:" + words.get(key));
}
}
System.out.println("测试结束,共答对 " + correct + " 个单词。");
}
public void displayUnknownWords() {
if (unknownWords.isEmpty()) {
System.out.println("生词本为空。");
} else {
System.out.println("生词本:");
for (String word : unknownWords) {
System.out.println(word + ":" + words.get(word));
}
}
}
public void saveToFile(String filename) {
try (PrintWriter writer = new PrintWriter(filename)) {
for (String key : words.keySet()) {
writer.println(key + "," + words.get(key));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void loadFromFile(String filename) {
try (Scanner scanner = new Scanner(new File(filename))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
words.put(parts[0], parts[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
```
这个程序实现了以下功能:
1. 可以根据需要添加单词,删除单词,展示单词,并标记生词加入生词本。
2. 可以进行单词测试,随机出题,让用户选择单词的含义。
3. 可以展示生词本中的生词。
4. 可以将单词保存到文件中,并从文件中读取单词。
您可以根据需要进一步修改程序并添加界面,使程序更加易用和美观。
阅读全文