当前位置:网站首页>C语言实现猜数字(附带源码,可直接运行)
C语言实现猜数字(附带源码,可直接运行)
2022-08-11 05:25:00 【Rserendipity】
初学者警告
初学者警告
初学者警告
重要的事说三遍,请轻喷哈
电子信息专业的大一学生,学校放寒假了,在家挺无聊的,并且下学期没有c语言的课程了,还是写点东西复习复习一下吧,免得都还给老师了(逃
#define _CRT_SECURE_NO_WARNINGS
//我用的编辑器是vs2019,防止scanf等函数报错,gcc、dev等编辑器可以去掉这一行
#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);//根据用户的输入经行选择开始游戏还是退出
switch (input)
{
case 1:
game();//调用游戏函数
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);//同时可以使用用户输入的值来控制do—while循环
}
void game()
{
int temp;
int input;
printf("请输入你想猜的数字的范围(左小右大,用空格隔开):>");
temp = generate_num();//随机数生成函数,得到随机值
do//进行do—while循环,直到用户输入正确的数停止
{
printf("请输入你猜的数:>");
scanf("%d", &input);
if (input == temp)
{
printf("猜对啦!\n");
break;
}
else if (input < temp)
{
printf("小啦小啦!\n");
}
else if (input > temp)
{
printf("大啦大啦!\n");
}
} while (1);
int a;
do//询问是否再玩一次
{
again();
scanf("%d", &a);
switch (a)
{
case 1:
game();//递归调用,重新开始游戏
case 0:
printf("退出游戏!\n");
exit(0);//退出程序
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (a);//同时可以使用用户输入的值来控制do—while循环
}
int generate_num()
{
int x, y, z;
srand((unsigned int)time(NULL) * 100);//使用时间戳来生成随机数字,输入范围较小时*100可以去掉
scanf("%d%d", &x, &y);//给定生成随机数的范围
if (x > y)//判断是否符合要求
{
printf("你输入的前一个数字比后一个数字大哦!\n");
printf("请重新输入:>");
generate_num();
}
else if (x == y)
{
printf("你输入两个数字一样哦!\n");
printf("请重新输入:>");
generate_num();
}
else
z = rand() % y + x;//生成随机数
return z;
}
注释都写在代码内了,本人是初学者,有什么不足的地方欢迎指正哈
边栏推荐
猜你喜欢

AI-based intelligent image recognition: 4 different industry applications

vscode插件开发——代码提示、代码补全、代码分析

使用ActiveReports制作第一张报表

推出 Space Marketplace 测试版 | 新发布

CMT2380F32模块开发10-高级定时器例程

Robust 3D Object Detection in Cold Weather Conditions

MSP430学习总结(二)——GPIO

10 个超好用的 DataGrip 快捷键,快加入收藏! | 实用技巧

Reconstruction and Synthesis of Lidar Point Clouds of Spray

Joint 3D Instance Segmentation and Object Detection for Autonomous Driving
随机推荐
The kernel communicates with user space through character devices
目标检测学习目录(持续更新)
CMT2380F32模块开发4-UART例程
STM32学习总结(一)——时钟RCC
vscode插件开发——代码提示、代码补全、代码分析
Argparse模块 学习
CMT2380F32模块开发11-RTC例程
华为云IOT平台设备获取api调用笔记
物联网基础知识学习
vscode插件开发——懒人专用markdown插件开发
win10 配置tensorflow(GPU) anaconda3 cuda9.0 cudnn for 9.0
The selection points you need to know about the helmet identification system
Safety helmet recognition - construction safety "regulator"
ActiveReports报表分类之页面报表
CVPR2020: Seeing Through Fog Without Seeing Fog
IIC 和 SPI
LAGRANGIAN FLUID SIMULATION WITH CONTINUOUS CONVOLUTIONS
STM32-中断优先级管理NVIC
目标检测——Faster-RCNN 之 RCNN
Diagnostic Log and Trace——开发人员如何使用 DLT