当前位置:网站首页>猜数字+随机数
猜数字+随机数
2022-07-21 20:31:00 【p29949】
#include<stdio.h>
//电脑随机生成一个1~100之间的数字
//猜数字
//如果你猜小了,告知猜小了
//如果你猜大了,告知猜大了
//如果你猜对了,告知猜对了
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("*********************************\n");
printf("********** 1. play ************\n");
printf("********** 0. exit ************\n");
printf("*********************************\n");
}
//rand函数可以生成随机数
//返回一个0~RAND_MAX(32767)
void game()
{
int guess = 0;
//1. 生成随机数
int ret = rand()%100+1;//0~99-->1~100
//printf("%d\n", ret);
//2. 猜数字
while (1)
{
printf("请猜数字:>");
scanf("%d", &guess);
if (guess < ret)
{
printf("猜小了\n");
}
else if (guess > ret)
{
printf("猜大了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
int main()
{
int input = 0;
//设置随机数的生成器
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择!\n");
break;
}
} while (input);
return 0;
}
边栏推荐
猜你喜欢
imdecode、imencode、.tofile、fromfile 读取并保存 & 中文路径的中文名称的文件 & 一步一步解析并对比函数的结果
BGP-边界网关协议
Service worker guide-1
Responsive dream weaving template smart home website
systemd 管理 redis-exporter linux
[shutter -- basic component] radio switch & Radio & checkbox
Restful URL design specification
What are the five standards for the dual prevention mechanism of hazardous chemical enterprises and what are the contents
Value and technical thinking of vectorization engine for HTAP
Creation and call of stored procedure based on Oracle Database
随机推荐
Is the flush platform safe? Huatai Securities Account Opening app
即看即用 && 比较操作(Comparison Ops) && Pytorch官方文档总结 && 笔记 (七)
Is it really so difficult to code under the eslint specification?
《时代》杂志重磅封面:元宇宙时代将改变世界
flask 启动函数返回值的剖析
【Flutter -- 基础组件】单选开关(Switch)& 单选框(Radio) & 复选框(Checkbox)
深入解析 pycocotools 的安装和运行报错 && 安装 mmcv-full and mmpycocotools
ESlint规范下编码真的这么难嘛?
关于编写安全的智能合约
Responsive dream weaving template smart home website
Value and technical thinking of vectorization engine for HTAP
TZC 1283: 简单排序 —— 快速排序
composer.json 常用配置解释
ICML 2022 lottery! Fudan University, Shanghai Jiaotong University and Xiamen University were selected for outstanding papers
AcWing 1124. 骑马修栅栏 题解(欧拉回路)
网络安全竞赛C模块批量拿值脚本
Service worker guide-1
Custom type: structure (II) bit segment implementation
B2B企业数字化转型,CIO如何避免踩坑
_TensorBase(45个张量基础后置函数总结) && Pytorch官方文件 && NOTEBOOK(NINE)