当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
开源之夏 2022 火热来袭 | 欢迎报名 OpenMLDB 社区项目~
OpenMLDB Pulsar Connector:高效打通实时数据到特征工程
CMT2380F32模块开发1-硬件
如何快速转行做产品经理
weex入门踩坑
Error: Flash Download failed - “Cortex-M4“-STM32F4
第四范式OpenMLDB优化创新论文被国际数据库顶会VLDB录用
栈stack
红外线一认识
物联网IOT 固件升级
实时姿态估计--基于空洞卷积的人体姿态估计网络
STM32学习总结(二)——GPIO
跨应用间调用: URL Scheme
Visual studio2019 配置使用pthread
CMT2380F32模块开发5-CLK例程
珍爱网App竞品分析报告
SWOT分析法
博客目录
vmware不可恢复错误vmui
Promise 中状态改变和回调执行先后顺序 和promise多次回调









