gpt4 book ai didi

c - 随机数游戏C

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:41 25 4
gpt4 key购买 nike

游戏在我第一次玩的时候运行良好,不过;第二次它只给了你两条生命......我试图改变生命的数量,但仍然无法弄清楚我做错了什么。

// C_program_random_number_game

#include<stdio.h>
#include<time.h>
#include <stdlib.h>

int main()
{

srand(time(NULL));
int num1,x = 0;
char game, cont, replay;

printf("Would you like to play a game? : ");
scanf("%c",&game);

if (game == 'y' || game == 'Y')
{
printf("\nThe rules are simple. You have have 5 tries to guess the computers number. \n \n If you succeed you win the game, if you dont you lose the game. Good luck!");
do
{
int r = rand()%5 +1;
printf("\n\nEnter a number between 1 and 5 : ");
scanf("\n%d",&num1);
x++;
if(num1 > 0 && num1 < 5)
{

do
{
if(num1 < r)
{
printf("\nClose! try a little higher... : ");
x++;
}
else if (num1 > r)
{
printf("\nClose! try a little lower...: ");
x++;
}
scanf("%d",&num1);

}while(num1!=r && x <3);

if(num1 == r)
{
printf("\nWinner! >> you entered %d and the computer generated %d! \n",num1, r);
}
else if(num1 != r)
{
printf("\nBetter luck next time!");
}
printf("\n\nWould you like to play again? (y or n) : ");
scanf("\n%c",&replay);
}
else
{
printf("Sorry! Try again : ");
scanf("%d",&num1);
}

}while(replay == 'y'|| replay == 'Y');

}
else if (game == 'n' || game == 'N')
{
printf("Okay, maybe next time! ");
}
else
{
printf("Sorry, invalid data! ");
}
return 0;

最佳答案

您的代码存在各种问题(其中大多数在编程方面都是次要的)。大多数错误是你想通过这个问题完成的事情和你 printf() 的错别字。

照原样,此代码将在 1-25 之间随机,接受任何有效 int 的输入,查看您是否匹配它,并且只给您 5 次尝试。 (我没有添加错误检查来强制输入在 1-25 之间。这可能应该被添加。)

我在下面对我的代码进行了所有更改,并按照您在 printf() 中所做的更改进行了注释。

注意:请参阅我上面的评论以了解我所做更改的解释,因为我已经指出了这些更改。我还对其进行了格式化,使其更易于阅读。

注意 2:我使用在线编译器快速完成了这项工作。如果您发现这有任何问题或无法正常工作,请在下方发表评论,我会解决。

// C_program_random_number_game

#include<stdio.h>
#include<time.h>
#include <stdlib.h>

int main()
{

srand(time(NULL));
int num1,x = 0;
char game, cont, replay;

printf("Would you like to play a game? : ");
scanf("%c",&game);

if (game == 'y' || game == 'Y')
{
printf("\nThe rules are simple. You have have 5 tries to guess the computers number. \n \n If you succeed you win the game, if you dont you lose the game. Good luck!");

do
{
int r = rand()%25 +1;

printf("\n\nEnter a number between 1 and 25 : ");
scanf("%d",&num1);

do
{
printf("r = %d\n", r);

if(num1 < r)
{
printf("\nClose! try a little higher... : ");
x++; //Increment x if wrong guess
}
else if (num1 > r)
{
printf("\nClose! try a little lower...: ");
x++; //Increment x if wrong guess
}

scanf("%d",&num1);
}while(num1!=r && x < 5); //If x is 5 or more, they ran out of guesses (also, you want an && not an ||)

if(num1 == r) //Only print "winner" if they won!
{
printf("\nWinner! >> you entered %d and the computer generated %d! \n",num1, r);
}

printf("\nWould you like to play again? (y or n) : ");
scanf("\n%c",&replay);
}while(replay == 'y'|| replay == 'Y');
}

printf("Thanks for playing! ");

if (game == 'n' || game == 'N')
{
printf("Okay, maybe next time! ");
}
return 0;
}

关于c - 随机数游戏C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765876/

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