gpt4 book ai didi

java - System.out.print不断循环错误

转载 作者:行者123 更新时间:2023-12-02 02:26:19 25 4
gpt4 key购买 nike

大家好,我一直在尝试帮助我的 friend 使用这段几乎可以工作的代码。这是一个简单的二十一点游戏,询问用户是否想要另一张牌,写"is",如果用户写“否”,那么他的回合结束,应该轮到庄家了。我遇到的问题是,当我写“否”来结束我的回合时,它只是不断询问我是否需要在控制台中添加另一张卡。谢谢你的帮助。例如。

Another Card (yes/no)?: nO
Another Card (yes/no)?: no
Another Card (yes/no)?: no
Another Card (yes/no)?: no
Another Card (yes/no)?: nO
Another Card (yes/no)?: no

这是我的代码:

while (playing == true ){
System.out.print("Another Card (yes/no)?: ");
anotherCard = keyboard.nextLine();


if (anotherCard.equals("yes")) {

nextCard = random.nextInt(10) + 1;
cardTotal += nextCard;
System.out.println("Card: " + nextCard);
System.out.println("Total: " + cardTotal);

if (cardTotal > 21) {
System.out.println("You busted, Dealer Wins");
playing = false;
}
if (cardTotal == 21) {
System.out.println("Player Wins");
playing = false;
}

}
if (anotherCard.equals("no")) {
dealerturn = true;

if ((dealerturn == true) && (dTotal < 17)) {
nextCard = random.nextInt(10) + 1;
dTotal += nextCard;

if (dTotal > 21) {
System.out.println("Dealer Busts, You Win!");
playing = false;

}
}
else if ((dealerturn == true) && (dTotal > 17)) {
if (dTotal > 21) {
System.out.println("Dealer Busts, You Win!");
playing = false;
}
}
}
}
}

最佳答案

这是因为当用户不想玩时,您没有将 playing 更改为包含 false:

if (anotherCard.equals("yes")) {
...
} else { // you should add this case:
playing = false;
}

关于java - System.out.print不断循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47724435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com