一、核心目标
新增两个实用功能:
1.给中文翻译,让用户拼写完整单词;
2. 给英文单词,从4个选项中选正确翻译,全程保持基础Java语法,步骤清晰无冗余。
二、代码编写步骤
第一步:先补充V1.0基础代码
先把V1.0中"导入工具类、创建主类、初始化工具/容器、选择单词表、读取文件数据"这5部分代码完整复制过来(和之前完全一致,无需修改),作为后续功能的基础。
第二步:新增"功能选择菜单"(让用户选刷题模式)
在"读取文件数据"之后、原来的"填字母逻辑"之前,添加菜单代码,让用户自主选择刷题方式:
java
// 功能选择菜单
System.out.println("\n请选择刷题模式:");
System.out.println("1-填缺失字母");
System.out.println("2-看中文拼英文");
System.out.println("3-看英文选中文(4选1)");
int mode = sc.nextInt(); // 接收用户选择的模式
第三步:实现"模式2-看中文拼英文"功能
在"功能选择菜单"之后,添加模式2的逻辑代码,用户输入完整单词后判断对错:
java
// 模式2:看中文,拼英文单词
if (mode == 2) {
int wordCount = words.size();
while (true) {
// 1. 随机选单词和对应中文
int randomIndex = ran.nextInt(wordCount);
String targetWord = words.get(randomIndex);
String targetChinese = chineseWords.get(randomIndex);
// 2. 提示用户输入英文
System.out.println("\n中文翻译:" + targetChinese);
System.out.println("请输入对应的英文单词:");
String userWord = sc.next(); // 接收用户输入的完整单词
// 3. 判断对错(忽略大小写,避免用户因大小写输错)
if (userWord.equalsIgnoreCase(targetWord)) {
System.out.println("太棒了!拼写正确~ 单词:" + targetWord);
} else {
// 答错可重复输入,直到正确
while (!userWord.equalsIgnoreCase(targetWord)) {
System.out.println("拼写错误!再试试:");
userWord = sc.next();
}
System.out.println("终于对啦!正确单词:" + targetWord);
}
}
}
第四步:实现"模式3-看英文选中文(4选1)"功能
在模式2代码之后,添加模式3的逻辑,生成3个干扰选项+1个正确选项,让用户选序号:
java
// 模式3:看英文,从4个选项中选正确中文
else if (mode == 3) {
int wordCount = words.size();
while (true) {
// 1. 随机选1个正确单词和翻译
int correctIndex = ran.nextInt(wordCount);
String correctEng = words.get(correctIndex);
String correctChinese = chineseWords.get(correctIndex);
// 2. 生成3个干扰选项(从其他单词中选,避免重复)
ArrayList<String> options = new ArrayList<>();
options.add(correctChinese); // 先加正确选项
while (options.size() < 4) {
int randomDisturb = ran.nextInt(wordCount);
String disturbChinese = chineseWords.get(randomDisturb);
// 确保干扰选项和正确选项不同,且不重复
if (!options.contains(disturbChinese) && randomDisturb != correctIndex) {
options.add(disturbChinese);
}
}
// 3. 打乱选项顺序(避免正确答案一直是第1个)
ArrayList<String> shuffledOptions = new ArrayList<>();
while (!options.isEmpty()) {
int randomPos = ran.nextInt(options.size());
shuffledOptions.add(options.remove(randomPos));
}
// 4. 显示题目和选项
System.out.println("\n英文单词:" + correctEng);
System.out.println("请选择正确的中文翻译(输入1-4的序号):");
for (int i = 0; i < shuffledOptions.size(); i++) {
System.out.println((i+1) + "-" + shuffledOptions.get(i));
}
// 5. 接收用户选择并判断
int userChoice = sc.nextInt();
// 转换用户输入的序号(1-4)为列表索引(0-3)
String userAnswer = shuffledOptions.get(userChoice - 1);
if (userAnswer.equals(correctChinese)) {
System.out.println("选择正确!🎉 中文翻译:" + correctChinese);
} else {
// 答错可重新选,直到正确
while (!userAnswer.equals(correctChinese)) {
System.out.println("选错啦!再试试~");
userChoice = sc.nextInt();
userAnswer = shuffledOptions.get(userChoice - 1);
}
System.out.println("对啦!正确答案是" + (shuffledOptions.indexOf(correctChinese)+1) + "-" + correctChinese);
}
}
}
第五步:保留"模式1-填缺失字母"功能
在模式3代码之后,添加原来的填字母逻辑,让三个模式都能使用:
java
// 模式1:填缺失字母(保留V1.0功能,无需修改)
else if (mode == 1) {
int wordCount = words.size();
while (true) {
int randomWordIndex = ran.nextInt(wordCount);
String selectedWord = words.get(randomWordIndex);
String selectedChinese = chineseWords.get(randomWordIndex);
int wordLength = selectedWord.length();
int randomCharIndex = ran.nextInt(wordLength);
char correctChar = selectedWord.charAt(randomCharIndex);
String frontPart = selectedWord.substring(0, randomCharIndex);
String backPart = selectedWord.substring(randomCharIndex + 1);
String wordWithBlank = frontPart + "_" + backPart;
System.out.println("\n请猜出单词中缺失的字母:" + wordWithBlank);
String userInput = sc.next();
char userChar = userInput.charAt(0);
if (userChar == correctChar) {
System.out.println("正确!完整单词:" + selectedWord + ",中文翻译:" + selectedChinese);
} else {
while (userChar != correctChar) {
System.out.println("错误!请重新输入:");
userInput = sc.next();
userChar = userInput.charAt(0);
}
System.out.println("终于对啦!完整单词:" + selectedWord + ",中文翻译:" + selectedChinese);
}
}
} else {
System.out.println("模式编号错误,程序退出~");
}
三、运行说明
- 运行程序后,先输入1001(CET4)或1002(CET6)选择单词表
- 再输入1-3选择刷题模式,按提示答题:
-
模式2:输入完整英文单词,大小写不影响判断;
-
模式3:输入选项序号(1-4),选错可重新选。
- 退出程序直接关闭运行窗口即可。
四、新增关键知识点
-
equalsIgnoreCase() :忽略大小写比较字符串,比如用户输"Java"和"java"都算对。
-
ArrayList.contains() :判断列表中是否包含某个元素,用来避免干扰选项重复。
-
选项打乱逻辑:通过"随机移除原列表元素并添加到新列表",实现选项顺序随机,更贴近真实刷题场景。