【发布时间】:2021-03-18 13:36:50
【问题描述】:
我正在做一个猜谜游戏,你应该猜 x 国家的首都,如果用户想再次猜,应该每次都提示用户有一个 15 个 CountryCards 的数组,我需要通读随机顺序,我也不能两次使用同一张卡。当我运行我当前的代码时,我完成了 14 次数组迭代,然后最后一次只是卡在 while 循环的最开始(在输入 1 到 yesNo 之后没有任何反应)这是相关代码,任何帮助表示赞赏:
while (CountryCard.instances < 30) {
System.out.println("Would you like to guess the capital of a country?"
+ " Hit 1 for yes, or 2 for no (Case sensitive)");
yesNo = input.nextInt();
if (yesNo == 2) {
return;
}
Random generator = new Random();
int randomNumber = generator.nextInt(game.length);
while (game[randomNumber].used == true && CountryCard.uses < 15) {
randomNumber = generator.nextInt(game.length);
}
System.out.println("What is the Capital of "
+ game[randomNumber].getName() + "?");
game[randomNumber].usedCard(1);
guess = input.next();
if (guess.equals(game[randomNumber].getCapital())) {
System.out.println("Correct! :)");
} else {
System.out.println("Incorrect! :(");
}
}//end of while
System.out.println("Thanks for playing :)");
【问题讨论】: