用java写一个背单词程序 要求如下“ 背单词 ”:界面可以再好看一点;可以去掉音标;可以改变单词显示的速度;可以增加标记生词并记到生词本中;可以增加测试的功能(单词含义可以随机选4个词的含义来让用户选择)等等。附件中有两个代码,一个单词本
时间: 2024-02-15 15:02:09 浏览: 105
好的,我了解了你的要求。以下是一个简单的背单词程序的Java代码示例,实现了你的要求:
```
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class WordLearningProgram {
private ArrayList<String> words;
private ArrayList<String> meanings;
private ArrayList<String> newWords;
private ArrayList<String> newMeanings;
private ArrayList<String> testWords;
private ArrayList<String> testMeanings;
private ArrayList<Integer> testChoices;
private Scanner scanner;
private Random random;
public WordLearningProgram() {
words = new ArrayList<>();
meanings = new ArrayList<>();
newWords = new ArrayList<>();
newMeanings = new ArrayList<>();
testWords = new ArrayList<>();
testMeanings = new ArrayList<>();
testChoices = new ArrayList<>();
scanner = new Scanner(System.in);
random = new Random();
}
public void run() {
loadWords();
while (true) {
System.out.println("请选择功能:");
System.out.println("1. 背单词");
System.out.println("2. 生词本");
System.out.println("3. 测验");
System.out.println("4. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
learnWords();
break;
case 2:
viewNewWords();
break;
case 3:
takeTest();
break;
case 4:
System.out.println("再见!");
return;
default:
System.out.println("请输入正确的选项!");
}
}
}
private void loadWords() {
// 加载单词本
// 将单词和含义分别存入words和meanings数组中
// 示例代码中使用了一个简单的单词本
words.add("apple");
meanings.add("苹果");
words.add("banana");
meanings.add("香蕉");
words.add("cherry");
meanings.add("樱桃");
}
private void learnWords() {
// 学习单词
int speed = getSpeed();
for (int i = 0; i < words.size(); i++) {
String word = words.get(i);
String meaning = meanings.get(i);
System.out.println(word + " " + meaning);
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println();
if (isNewWord(word)) {
System.out.println("是否将其加入生词本?(Y/N)");
String answer = scanner.next();
if (answer.equalsIgnoreCase("Y")) {
newWords.add(word);
newMeanings.add(meaning);
System.out.println("已加入生词本!");
}
}
}
System.out.println("单词已全部背完!");
}
private int getSpeed() {
// 获取单词显示速度
System.out.println("请输入单词显示速度(毫秒):");
int speed = scanner.nextInt();
return speed;
}
private boolean isNewWord(String word) {
// 判断是否为生词
return !newWords.contains(word);
}
private void viewNewWords() {
// 查看生词本
if (newWords.isEmpty()) {
System.out.println("生词本为空!");
return;
}
System.out.println("生词本:");
for (int i = 0; i < newWords.size(); i++) {
System.out.println(newWords.get(i) + " " + newMeanings.get(i));
}
}
private void takeTest() {
// 测验
if (words.size() < 4) {
System.out.println("单词数量不足,无法进行测验!");
return;
}
generateTestWords();
System.out.println("请选择以下单词的含义:");
for (int i = 0; i < testWords.size(); i++) {
System.out.println((i + 1) + ". " + testWords.get(i));
}
int answer = scanner.nextInt();
if (answer == testChoices.get(answer - 1)) {
System.out.println("回答正确!");
} else {
System.out.println("回答错误!");
}
}
private void generateTestWords() {
// 生成测验单词
testWords.clear();
testMeanings.clear();
testChoices.clear();
for (int i = 0; i < 4; i++) {
int index = random.nextInt(words.size());
String word = words.get(index);
String meaning = meanings.get(index);
testWords.add(word);
testMeanings.add(meaning);
testChoices.add(i + 1);
}
int correctIndex = random.nextInt(4);
testWords.set(correctIndex, words.get(correctIndex));
testMeanings.set(correctIndex, meanings.get(correctIndex));
testChoices.set(correctIndex, correctIndex + 1);
shuffleTestChoices();
}
private void shuffleTestChoices() {
// 打乱测验选项的顺序
for (int i = 0; i < testChoices.size(); i++) {
int j = random.nextInt(testChoices.size());
int temp = testChoices.get(i);
testChoices.set(i, testChoices.get(j));
testChoices.set(j, temp);
}
}
}
```
该程序使用了ArrayList来存储单词和含义,可以根据需要添加、删除和修改单词。用户可以选择背单词、查看生词本和进行测验三个功能。在背单词功能中,用户可以设置单词显示速度,并且可以将生词加入生词本中。在测验功能中,程序会随机选择四个单词,其中一个为正确答案,用户需要选择正确的含义。
阅读全文