当前位置:网站首页>C language implementation guess Numbers (with source code, can be directly run)
C language implementation guess Numbers (with source code, can be directly run)
2022-08-11 06:33:00 【Rserendipity】
Beginner warning
Beginner warning
Beginner warning
重要的事说三遍,please spray
A freshman majoring in electronic information,学校放寒假了,It's boring at home,And not next semesterclanguage courses,Or write something to review and review,Don't give it back to the teacher(逃
#define _CRT_SECURE_NO_WARNINGS
//我用的编辑器是vs2019,防止scanfWait for the function to report an error,gcc、devWait for the editor to remove this line
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//函数声明
void test();
void again();
void menu();
void game();
int generate_num();
void menu()
{
printf("\n");
printf("****************************\n");
printf("***** 猜数字 ****\n");
printf("***** 请输入1或者0 ****\n");
printf("***** 1.开始 0.退出 ****\n");
printf("****************************\n");
printf("\n");
}
void again()
{
printf("\n");
printf("想再玩一次吗?\n");
printf("输入1再玩一次,输入0退出游戏:>");
}
int main()
{
test();//调用test函数
return 0;
}
void test()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);//According to the user's input through the line to choose to start the game or quit
switch (input)
{
case 1:
game();//调用游戏函数
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);//It can also be controlled using user-entered valuesdo—while循环
}
void game()
{
int temp;
int input;
printf("Please enter the range of numbers you want to guess(左小右大,用空格隔开):>");
temp = generate_num();//随机数生成函数,得到随机值
do//进行do—while循环,Stops until the user enters the correct number
{
printf("请输入你猜的数:>");
scanf("%d", &input);
if (input == temp)
{
printf("猜对啦!\n");
break;
}
else if (input < temp)
{
printf("Small, small!\n");
}
else if (input > temp)
{
printf("Big, big, big!\n");
}
} while (1);
int a;
do//Ask if you want to play again
{
again();
scanf("%d", &a);
switch (a)
{
case 1:
game();//递归调用,重新开始游戏
case 0:
printf("退出游戏!\n");
exit(0);//退出程序
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (a);//It can also be controlled using user-entered valuesdo—while循环
}
int generate_num()
{
int x, y, z;
srand((unsigned int)time(NULL) * 100);//Use timestamps to generate random numbers,When the input range is small*100可以去掉
scanf("%d%d", &x, &y);//Given a range for generating random numbers
if (x > y)//判断是否符合要求
{
printf("The previous number you entered is greater than the next number!\n");
printf("请重新输入:>");
generate_num();
}
else if (x == y)
{
printf("You enter two numbers the same!\n");
printf("请重新输入:>");
generate_num();
}
else
z = rand() % y + x;//生成随机数
return z;
}
Comments are written in the code,本人是初学者,If there is any inadequacy, please correct me
边栏推荐
- STM32F4-正点原子探索者-SYSTEM文件夹下的delay.c文件内延时函数详解
- Node-3.构建Web应用(一)
- 场景驱动的特征计算方式OpenMLDB,高效实现“现算先用”
- 何凯明新作ViTDET:目标检测领域,颠覆分层backbone理念
- Thesis unscramble TransFG: A Transformer Architecture for Fine - grained Recognition
- 珍爱网App竞品分析报告
- 活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光
- 【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
- 防盗链——防止其他页面通过url直接访问本站资源
- OpenMLDB官网升级,神秘贡献者地图带你快速进阶
猜你喜欢
随机推荐
stm32-WS2812 PWM+DMA(自己写库函数)
红外线一认识
Interpretation of the paper: Cross-Modality Fusion Transformer for Multispectral Object Detection
CMT2380F32模块开发8-Base Timer例程
黑马大事件项目
2021-09-11 C语言 变量与内存分配
Error: Flash Download failed - “Cortex-M4“-STM32F4
Diagnostic Log and Trace——dlt的编译和安装
C语言实现猜数字(附带源码,可直接运行)
net6 的Web MVC项目中事务功能的应用
网络七层结构(讲人话)
C语言的编译
OpenMLDB官网升级,神秘贡献者地图带你快速进阶
scanf函数在混合接受数据(%d和%c相连接)时候的方式
论文解读:跨模态/多光谱/多模态检测 Cross-Modality Fusion Transformer for Multispectral Object Detection
10 个超好用的 DataGrip 快捷键,快加入收藏! | 实用技巧
Asp doNet Mvc4绑定js脚本用法
USB中用NRZI来编码数据
华为IOT设备消息上报和消息下发验证
js常用方法对象及属性









