用java写一个背单词程序,要求可以改变单词显示的速度;可以增加标记生词并记到生词本中;可以增加测试的功能(单词含义可以随机选4个词的含义来让用户选择)等等,写出该代码
时间: 2024-02-06 15:09:48 浏览: 67
以下是一个简单的背单词程序示例代码,实现了速度控制、生词本和测试功能。你可以根据自己的需求进行修改和优化。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class WordLearningProgram {
private ArrayList<String> wordList;
private ArrayList<String> newWords;
private int speed;
public WordLearningProgram() {
wordList = new ArrayList<String>();
newWords = new ArrayList<String>();
speed = 3; // 默认速度为3秒每个单词
}
public void addWord(String word) {
wordList.add(word);
}
public void addNewWord(String word) {
newWords.add(word);
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void startLearning() {
Scanner scanner = new Scanner(System.in);
int count = 0;
for (String word : wordList) {
System.out.print(word);
try {
Thread.sleep(speed * 1000); // 控制显示速度
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("\r"); // 清除单词
System.out.print(String.format("%" + word.length() + "s", "").replaceAll(" ", "-")); // 显示单词长度的横线
System.out.print("\r");
count++;
if (count % 10 == 0) { // 每10个单词暂停一次
System.out.print("按任意键继续...");
scanner.nextLine();
}
if (newWords.contains(word)) { // 如果是生词,添加到生词本中
System.out.print("生词!是否添加到生词本?(y/n)");
String input = scanner.nextLine();
if (input.equals("y")) {
newWords.remove(word);
System.out.println("已添加到生词本。");
}
}
}
System.out.println("已完成所有单词的学习。");
}
public void startTesting() {
Scanner scanner = new Scanner(System.in);
int correctCount = 0;
while (true) {
Collections.shuffle(wordList); // 随机单词顺序
for (String word : wordList) {
System.out.println("请选出单词 " + word + " 的含义:");
ArrayList<String> options = new ArrayList<String>();
options.add(word); // 正确答案
while (options.size() < 4) { // 随机选出3个错误答案
String randomWord = wordList.get((int) (Math.random() * wordList.size()));
if (!options.contains(randomWord)) {
options.add(randomWord);
}
}
Collections.shuffle(options); // 随机选项顺序
for (int i = 0; i < options.size(); i++) {
System.out.println((i + 1) + ". " + options.get(i));
}
int answer = 0;
while (true) {
System.out.print("请输入答案编号:");
try {
answer = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("请输入数字!");
continue;
}
if (answer >= 1 && answer <= 4) {
break;
} else {
System.out.println("请输入1-4之间的数字!");
}
}
if (options.get(answer - 1).equals(word)) {
System.out.println("回答正确!");
correctCount++;
} else {
System.out.println("回答错误!正确答案为:" + word);
}
}
System.out.println("测试结束,共回答了 " + wordList.size() + " 题," + correctCount + " 题回答正确。");
System.out.print("是否重新测试?(y/n)");
String input = scanner.nextLine();
if (input.equals("n")) {
break;
}
}
}
public static void main(String[] args) {
WordLearningProgram program = new WordLearningProgram();
program.addWord("apple");
program.addWord("banana");
program.addWord("cherry");
program.addWord("date");
program.addNewWord("elderberry");
program.addNewWord("fig");
program.setSpeed(5);
program.startLearning();
program.startTesting();
}
}
```
阅读全文