栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

拿博客当笔记本——猜数字游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

拿博客当笔记本——猜数字游戏

//电脑随机生成一个1~100之间的数字

//猜数字

//玩家猜小了,电脑会告诉:猜小了

//玩家猜大了,电脑会告诉:猜大了

//玩家猜对了,电脑会告诉:恭喜你,猜对了

//C语言中生成的随机数的方式是rand函数

//0~RAND_MAX 0x7fff

//0~32767

#include //srand 函数的头文件

#include "time.h"

#include
#include
#include "time.h"

void menu()
{
    printf("******************n");
    printf("****** 1.play ****n");
    printf("****** 0.esit ****n");
    printf("******************n");

}

void game()
{
    int guess = 0;
    int a = rand() % 100 + 1;
    
    while (1)
    {
        printf("猜数字->n");
        scanf("%d", &guess);
        if (guess < a)
        {
            printf("猜小了n");
        }
        else if (guess > a)
        {
            printf("猜大了n");
        }
        else
        {
            printf("猜对了!!!n");
            break;
        }
    }
}
int main()
{
    int input = 0;

    do
    {
        srand((unsigned int)time(NULL));//时间-设置随机数的生成器 函数用在main函数里面 少运行几次
        menu();//打印菜单
            scanf("%d", &input);
        switch (input)
        {
        case 1:game();
               break;
        case 0:printf("游戏结束n");
               break;
        default:printf("输入无效n ");
            break;
        }
    }
    while (input);
        
        return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384723.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号