public void startGame() { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择游戏模式(1-娱乐模式,2-困难模式,0-退出):"); int mode = scanner.nextInt(); if (mode == 0) { break; } List<ChengYu> chengYuList; if (mode == 1) { chengYuList = commonChengYuList; } else { chengYuList = fullChengYuList; } Collections.shuffle(chengYuList); // 打乱顺序 int score = 0; boolean success = true; for (ChengYu cy : chengYuList) { String currentIdiom = cy.getChengYu(); for (ChengYu chengYu : commonChengYuList) if(chengYu.getChengYu().startsWith((currentIdiom.substring(currentIdiom.length() - 1)))) break; System.out.println("请回答以下成语的下一个成语:" + currentIdiom + "(难度:" + cy.getDifficultyLevel().getName() + ")"); String answer = scanner.next(); if (!cy.isCorrectAnswer(answer,commonChengYuList)) { System.out.println("回答错误!"); success = false; getHint(currentIdiom); continue; } else { score++; } if (success) { System.out.println("恭喜您回答正确,得分:" + score); for (ChengYu nextChengYu : fullChengYuList) if(nextChengYu.getChengYu().startsWith((answer.substring(answer.length() - 1)))) currentIdiom=nextChengYu.getChengYu(); } } } scanner.close(); }修改代码,要求回答正确后,系统以我的成语接龙出下一题
时间: 2024-03-04 17:53:18 浏览: 129
java人机猜拳-石头剪刀布.docx
public void startGame() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择游戏模式(1-娱乐模式,2-困难模式,0-退出):");
int mode = scanner.nextInt();
if (mode == 0) {
break;
}
List<ChengYu> chengYuList;
if (mode == 1) {
chengYuList = commonChengYuList;
} else {
chengYuList = fullChengYuList;
}
Collections.shuffle(chengYuList); // 打乱顺序
int score = 0;
boolean success = true;
ChengYu currentChengYu = null;
for (ChengYu cy : chengYuList) {
String currentIdiom = cy.getChengYu();
for (ChengYu chengYu : commonChengYuList) {
if(chengYu.getChengYu().startsWith((currentIdiom.substring(currentIdiom.length() - 1)))) {
currentChengYu = chengYu;
break;
}
}
System.out.println("请回答以下成语的下一个成语:" + currentIdiom + "(难度:" + cy.getDifficultyLevel().getName() + ")");
String answer = scanner.next();
if (!cy.isCorrectAnswer(answer,commonChengYuList)) {
System.out.println("回答错误!");
success = false;
getHint(currentIdiom);
continue;
} else {
score++;
if (success) {
for (ChengYu nextChengYu : commonChengYuList) {
if(nextChengYu.getChengYu().startsWith((answer.substring(answer.length() - 1)))) {
currentChengYu = nextChengYu;
break;
}
}
}
}
}
System.out.println("恭喜您回答正确,得分:" + score);
}
scanner.close();
}
修改后的代码中,增加了一个变量currentChengYu,用来记录当前成语,每次回答正确后,都根据当前成语找到下一个成语,并将其赋值给currentChengYu。然后在下一次循环中,输出当前成语,并让用户回答。
阅读全文