c语言猜数字游戏源程序

2025-04-04 19:46:33
推荐回答(3个)
回答1:

#include
#include

int main()
{
int s, a;
int f = 1;

while (f) {
system("cls");//DOS清屏命令
printf("操作说明:\n\t输入1继续游戏,0退出游戏,-1直接显示本次结果\n\n");
s = rand() % 1000;//生成随机数0-1000之内
printf("请输入您所猜的数字(0-1000) \n");
scanf("%d", &a);

while (1) {
if (a == s) {
printf("恭喜您猜测成功!\n");
break;
}else if (a > s) {
printf("高了!请再次输入:");
scanf("%d", &a);
}else if (a< s) {
printf("低了!请再次输入:");
scanf("%d", &a);
}

if (a == -1) {
printf("%d", s);
break;
}
}
printf("提示:\n\t1.重新开始游戏,0.退出游戏:\n请输入:");
scanf("%d", &f);
}
return (0);
}

回答2:


#include

#include

#include

#include

 

 

 

 

main(){

 

    int count , number , guess ;

 

    char yes = 'Y';

 

 

 

    printf("\n now let us play the game ,\n Guess the number");

 

 

    srand((int)time(0));

    number = 1+(int)(100.0*rand()/(RAND_MAX+1.0));

 

    //如果想偷懒 这里显示答案

    //printf("%d\n", number);

 

    count=0;

 

 

    do{

 

 

            do{

 

                printf("\n Input an integer number(1-100): ");

                scanf("%d",&guess);

 

 

 

            }while(!(guess>1 && guess <=100));

 

             

             

 

            if (guess==number)

            {

 

                printf("\ngongxi ni caidui le  thank you");

                yes ='N';

 

 

                number = 1+(int)(100.0*rand()/(RAND_MAX+1.0));

 

                //如果想偷懒 这里显示答案

                //printf("%d\n", number);

 

 

                printf("\nNEXT?(Y/N):");

                scanf("%s",&yes);

 

            }else{

             

 

             

                do

                {

                    if (guess

                    printf("\nYour answer is LOW ,try again\n");

 

                    if (guess>number)

                    printf("\nYour answer is high ,try again\n");

 

                    count++;

                     

 

 

 

                } while (guess==number);

 

                if (count==15)

                {

                        printf("\n That is the %d time ! think is hard next!");

                        exit(0);

                }

 

                if (count<=7)

                {

                        printf("\n You have got it in %d times \n",count);

                        printf("\n Congretulations");

                }else{

 

                        printf("\n You got it in %d times \n",count);

                        printf("\n bet you can do it better");

 

                }

 

            }

 

 

    }while(toupper(yes)=='Y');

 

 

 

}

回答3:

# include
# include
# include
main()
{
int a,b;
char c;
srand(time(NULL));//设置一个随机种子,每次运行都能保证随机种子不同
a=1+(rand()%1000);//随机产生1000以内的数
printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");
scanf("%d",&b);//要求输入一个数
while(b!=-1)
{
if(b==a)//判断输入的数是否和随机产生的数相同。
{
printf("Excellent! You guessed the number!\nWould you like to play again(y or n)?");
scanf("%c",&c);
scanf("%c",&c);
switch(c){
case 'y':
printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");
scanf("%d",&b);
break;
case 'n':
break;
}
}

while(b{
printf("Too low.Try again.");
scanf("%d",&b);
}

while(b>a)//比较两个数大小
{
printf("Too high.Try again.");
scanf("%d",&b);
}
}
}