gpt4 book ai didi

c - 使用 if 语句的循环在 C 中无法正常工作

转载 作者:行者123 更新时间:2023-11-30 20:48:41 26 4
gpt4 key购买 nike

我正在尝试编写一个包含一些 if 语句的循环,循环的问题是用户将输入一个选择,要么 H 要么 S 。但是,当用户输入 S 时,程序会直接转到 H 语句并执行该语句,然后再转到另一个语句。让我向您展示代码:

int decision(struct CardDeck *deck) {
char choice;
printf("Value is %d\n", deck->ValueOfSecondPlayer);
while (deck->ValueOfSecondPlayer <= 21)
{
if (deck->ValueOfSecondPlayer == 21) {
printf("Blackjack!");
}
if (deck->ValueOfSecondPlayer > 21) {
printf("Sorry, you lose");
}
if (deck->ValueOfSecondPlayer < 21) {
printf("Would you like to hit or stand?\n");
scanf("%c", &choice);
}
if (choice == 'H' || choice == 'h'); {
printf("You wish to hit; here's your card\n");
deck->ValueOfSecondPlayer += printCards(deck);
printf("Your total is now %d\n", deck->ValueOfSecondPlayer);
}
if (choice == 'S' || choice == 's') {
printf("You wished to stay\n");
break;
}
}
}

所以当我输入 S 时得到的输出是:

Value is 18
Would you like to hit or stand?
S
You wish to hit; here's your card
Jack of Clubs
Your total is now 28
You wished to stay
Pess any key to continue . . .

当我输入 H 时,程序运行如下:

Value is 16
Would you like to hit or stand?
h
You wish to hit; here's your card
4 of Clubs
Your total is now 20
Would you like to hit or stand?
You wish to hit; here's your card
2 of Spades
Your total is now 22
Press any key to continue . . .

所以我的问题是,即使我使用 Hh ,程序也会进入选择 Ss 。如果有人能告诉我我做错了什么以及如何解决问题,我将不胜感激!

最佳答案

在 h 的 if 语句后面有一个分号。

关于c - 使用 if 语句的循环在 C 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34561503/

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