所以我现在正在学习编码。这是一个在linux上的终端上运行的程序。
游戏应生成2-14之间的数字(卡片组),并让用户猜测下一个数字是高还是低。
我遇到的问题是游戏运行正常,但游戏会评估前一回合的数字。如果我在第一回合得到6,在第二回合得到7,在第三回合得到3,我猜测第三回合的数字会更高,它将被评估为正确的猜测因为7高于6,而不是将3考虑在内应该考虑到它。
当输入错误时,我也会遇到代码创建新随机数的问题。它应该保持相同的数字,直到用户输入有效的选项。
示例输出:
目前的牌是9。 下一个数字会更高(1)还是更低(2)? 2
你猜错了。 你目前的分数是-1! 目前的数字是9。 下一个数字会更高(1)还是更低(2)? 2
卡片是一样的。 你目前的分数是-1! 目前的数字是3 下一个数字会更高(1)还是更低(2)?
这是我的整个代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
/*declare variables*/
int pastCard;
int currentCard;
int score;
int userChoice;
int playGame = 1;
/*set up random*/
int range;
srand(time(NULL));
range = (13 - 1) + 1;
pastCard = rand() % range + 2;
currentCard = rand() % range + 2;
while (playGame == 1)
{
/*change the current card to the past card before creating new current card*/
pastCard = currentCard;
/*generate a random int for card*/
currentCard = rand() % range + 2;
if (currentCard < 11)
{
printf("The current card is a %d.\n", currentCard);
}
else if (currentCard == 11)
{
printf("The current card is a jack.\n", currentCard);
}
else if (currentCard == 12)
{
printf("The current card is a queen.\n", currentCard);
}
else if (currentCard == 13)
{
printf("The current card is a king.\n", currentCard);
}
else if (currentCard == 14)
{
printf("The current card is an ace.\n", currentCard);
}
printf("Will the next card be higher(1) or lower(2)? (press 0 to quit)\n");
scanf("%d", &userChoice);
printf("\n");
if (userChoice == 1)
{
if (currentCard > pastCard)
{
score++;
printf("You have guessed correctly.\n");
printf("Your current score is %d!\n", score);
}
else if (currentCard < pastCard)
{
score--;
printf("You have guessed incorrectly.\n");
printf("Your current score is %d!\n", score);
}
else if (currentCard == pastCard)
{
printf("The cards are the same.\n");
printf("Your current score is %d!\n", score);
}
}
else if (userChoice == 2)
{
if (currentCard < pastCard)
{
score++;
printf("You have guessed correctly.\n");
printf("Your current score is %d!\n", score);
}
else if (currentCard > pastCard)
{
score--;
printf("You have guessed incorrectly.\n");
printf("Your current score is %d!\n", score);
}
else if (currentCard == pastCard)
{
printf("The cards are the same.\n");
printf("Your current score is %d!\n", score);
}
}
else if (userChoice == 0)
{
playGame = 0;
printf("Final score: %d\n", score);
score = 0;
printf("Play again? (press 1 for yes, 0 for no)\n");
scanf("%d", &playGame);
printf("\n");
}
else
{
printf("Please enter a valid choice.\n");
}
}
return 0;
}
请帮忙!这让我非常沮丧!
答案 0 :(得分:1)
您可以通过更改以下内容来解决主要问题:
printf("The current number is a %d.\n", currentCard);
为:
printf("The current number is a %d.\n", pastCard);
在所有地方,因为在你写完之后,currentCard
就是他们想要猜测的那个,而不是你想要展示的那个。您可以通过将pastCard
重命名为currentCard
,将currentCard
重命名为nextCard
来改进您的计划。
或者,使用您更新的代码,请更改为:
if (pastCard < 11) {
printf("The current card is a %d.\n", pastCard);
} else if (pastCard == 11) {
printf("The current card is a jack.\n", pastCard);
} else if (pastCard == 12) {
printf("The current card is a queen.\n", pastCard);
} else if (pastCard == 13) {
printf("The current card is a king.\n", pastCard);
} else if (pastCard == 14) {
printf("The current card is an ace.\n", pastCard);
}
答案 1 :(得分:0)
跟踪卡片时出现逻辑错误。您向用户显示了错误的卡片。你想要显示当前卡,等待输入,然后选择一张新卡。
此外,在验证用户输入时,您可以执行以下循环:
while user input not valid
prompt for input
scan input
if input not valid then
print error message
end while
只有在这个循环之后,你才能继续生成新卡并进行逻辑检查。