首页 > 代码库 > 猜数游戏

猜数游戏

/*猜数游戏,magic number#4,版本*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
int magic;/*magic number*/
int guess;/*user‘s number*/
int guessnum,response=1;
time_t t;
printf("\nWelcome to the magic number game\n");
srand((unsigned int)time(&t));/*产生随机数*/
do
{
magic=rand()%101;/*产生1到100之间的随机数*/
guessnum=1;/*guessnum的初始值为1*/
while(guessnum<=3)/*未猜满3次,继续猜*/
{
printf("\nGuess the magic number:");
scanf("%d",&guess);/*输入第一个猜的数*/
if(guess==magic)
{

printf("***Right***\n");
printf("%d is the magic number\n",magic);
break;/*如果猜对了则跳出这个循环*/
}
else if(guess>magic)
printf("Wrong,too high\n");
else
printf("Wrong,too low\n");
guessnum++;/*猜错一次,guessnum便加1*/
}
if(guessnum>3)/*猜满3次则游戏结束*/
printf("Three times are wrong!\n");
printf("\nEnter ‘0‘ to quit or other to start a new game or:");
scanf("%d",&response);
}while(response!=0);
printf("\nGame Over!\n");
system("PAUSE");
return 0;

} /*end main*/

技术分享

猜数游戏