C语言实现猜数游戏
作者:互联网
让计算机来想一个数,然后让用户来猜。 用户每输入一个数字,就告诉他是大了还是小了, 直到用户猜中为止,最后还要告诉用户它猜了多少次。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(0));
int n=rand()%100+1; //初始化一个随机数n
int count=0; //初始化猜测次数
int a; //初始化用户要输入的数
printf("Computer had figured out a number from 1 to 100.\n");
do {
printf("Please guess this number.\n");
scanf("%d",&a);
count++;
if(a>n){
printf("Your guess is too big.\n");
}
if(a<n){
printf("Your guess is too small.\n");
}
}while(a!=n);
printf("You guessed the result %d times",count);
return 0;
}
标签:初始化,number,游戏,猜数,int,用户,C语言,printf,include 来源: https://blog.csdn.net/zznight/article/details/122409699