0
点赞
收藏
分享

微信扫一扫

C语言实现猜数游戏

自由的美人鱼 2022-01-10 阅读 74
让计算机来想一个数,然后让用户来猜。
用户每输入一个数字,就告诉他是大了还是小了,
直到用户猜中为止,最后还要告诉用户它猜了多少次。
#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;
}

 

举报

相关推荐

0 条评论