c语言猜数字小游戏



/***************************************************************************
* 功能:                                                                                               *
*      猜数字小游戏,分为3个难度等级,                                          *
*      难度1为1~100之间猜                                                                *
*      难度2为1~1000之间猜                                                              *
*      难度3为1~10000之间猜                                                            *
*      统计猜的次数                                                                            *
*说明:                                                                                                *
*    根据用户选择的难度等级,随机产生一个相应范围内的数字      *
*    用户输入猜的数字验证是否和随机产生的相等                            *
****************************************************************************/

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


int main(int argc,char *argv[])
{
    int mysteryNumber = 0;
    int guessNumber = 0;
    int max = 100,min = 1;
    int count = 1;
    int difficultyLevel = 0;

    printf("please input 1~3 for Difficulty Level\n");
    scanf("%d",&difficultyLevel);

    switch(difficultyLevel)
    {
        case 1:
        {
            min = 1;           
            max = 100;
            break;
        }
        case 2:
        {
            min = 1;           
            max = 1000;
            break;
        }       
        case 3:
        {
            min = 1;           
            max = 10000;
            break;
        }
        default:
            printf("input error,please input 1~3 for Difficulty Level\n");
            return -1;
            break;
    }

    srand(time(NULL));
    mysteryNumber = (rand() % (max - min + 1)) + min;
   
    do
    {
        switch(difficultyLevel)
        {
            case 1:
            {
                printf("input 1~100 a number\n");
                scanf("%d",&guessNumber);
                break;
            }
            case 2:
            {
                printf("input 1~1000 a number\n");
                scanf("%d",&guessNumber);
                break;
            }
            case 3:
            {
                printf("input 1~10000 a number\n");
                scanf("%d",&guessNumber);
                break;
            }
            default:
                break;
        }
        if(mysteryNumber > guessNumber)
        {
            ++count;
            printf("input number too small\n");
        }
        else if(mysteryNumber < guessNumber)
        {
            ++count;
            printf("input number too large\n");
        }
        else
        {
            printf("very good,you guessed correct\n");
            printf("you totally guess %d times\n",count);
        }
    }while(mysteryNumber != guessNumber);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值