所以我现在只是在学习编码。这是一个在 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;
}
请帮忙!这让我非常沮丧!