循环使用if语句在C

时间:2016-01-02 00:39:25

标签: c loops if-statement

我正在尝试编写一个包含一些if语句的循环,循环的问题是用户将输入一个选项H或{{1} }。但是,当用户输入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 . . . H,该程序仍会选择hS。 如果有人能告诉我我做错了什么以及如何解决问题,我将不胜感激!

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

请参阅评论中的注释。

mediaPlayer.setDataSource(context, musicUri);