当前位置:网站首页>C语言实现猜数字游戏
C语言实现猜数字游戏
2022-06-09 19:18:00 【来自爪哇的bean】
猜数字游戏
- 有一个简单的游戏首页
- 在每次玩家开始游戏时可产生随机数
- 在玩家输入所猜数字时,提示猜大了还是猜小了
- 在玩家输入所猜数字并确认时进行清屏
//C语言实现猜数字游戏
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int input; //存储选择的选项对应的数字
srand((unsigned)time(NULL)); //用时间戳来设置随机数的生成起点
//如果直接用rand去生成随机数的话,生成的随机数是伪随机数
do
{
home(); //游戏首页 单独写的一个函数
printf("请选择:>\n");
scanf("%d", &input);
system("cls"); //进行清屏
switch (input)
{
case 1:
game(); //进入游戏
break;
case 0:
break;
default:
printf("输入错误\n");
break;
}
} while (input);
}
int game()
{
int input = 0; //可以直接将input初始化,然后用input去存储猜的数字
//也可以重新定义一个变量来存储
int s; //用来接收所产生的随机数
s = rand();
while (1)
{
printf("请输入你猜的数字\n");
scanf("%d", &input);
system("cls"); //进行清屏
if (input > s)
{
printf("猜大了\n");
}
else if (input < s)
{
printf("猜小了\n");
}
else
{
printf("#########################################\n");
printf("#### Victory !!! ####\n");
printf("#########################################\n");
break;
}
}
}
int home()
{
printf("\n########################################\n");
printf("#### 1.开始游戏 ####\n");
printf("#### 0.退出游戏 ####\n");
printf("########################################\n");
}
运行结果



因为产生的随机数范围太大(0--32767之间),我们可以将它的范围缩小
实现代码
s = rand()%100+1; 从而将产生的随机数范围控制在0--100之间
边栏推荐
- Samsung completely closed the LCD production line, and 300 employees were transferred to the chip packaging Department
- GNN如何泛化?这篇135页pdf博士论文《神经图推理与学习的泛化评估与改进》
- 音频 3A 处理实践,让你的应用更「动听」
- 快速求完全二叉树的节点个数
- Investment of 550million yuan! Merck announced to build an advanced semiconductor integration base in Zhangjiagang, China
- 10个常见触发IO瓶颈的高频业务场景
- 企业内部Wiki,你建立了么?
- 龙芯胡伟武:克服奴才心态,做自己的CPU指令系统!LoongArch将与X86/Arm并驾齐驱
- Learn wmx3, motion control
- US Secretary of Commerce: consider adding more Chinese enterprises to the "blacklist" and will not relax sanctions in the near future
猜你喜欢

24个月暴涨180万名开发者,Rust 迎来高光时刻
MySQL 8.0.27 installation and configuration tutorial under Windows

第一次畫板子小記

Node版本切换

Desai wisdom number - line chart (smooth line chart): number of college entrance examination students in each province in 2022

Wechat applet - Basics

使用nvm下载安装Node

Why is cloud native data Lake worth paying attention to?
MySQL has no service and the ultimate solution sharing that the service cannot be started

Ieda installing the actibpm plug-in
随机推荐
5 longest palindrome substring (interval DP)
个人养老保险预期收益比较高的产品有哪些?
Deep learning and CV tutorial (13) | target detection (SSD, Yolo Series)
Technology sharing | selenium multi browser processing
20XX年全国职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
Electron FAQ 60 - error: could not start audio source
How to generate a QR code picture (add a Chinese logo) and save it to the specified picture static service address, so as to solve the problem that there are no QR code Chinese characters.
Electron常见问题 60 - 报错:Could not start audio source
Learn wmx3, motion control
10个常见触发IO瓶颈的高频业务场景
音频 3A 处理实践,让你的应用更「动听」
美国商务部长:考虑将更多中企加入“黑名单”,近期不会放松制裁
How does GNN generalize? This 135 page PDF doctoral thesis "generalization evaluation and improvement of neurograph reasoning and learning"
左手技术,右手生态!英特尔能否赢下这场“数据”战争?
The white paper on the panorama of the digital economy, bank wealth management, was released
[database data recovery] data recovery case of SQLSERVER database file missing for unknown reasons under Windows Server Environment
How about JPMorgan futures? Is it safe to open futures accounts?
《微信小程序-基础篇》初识微信小程序
低代码分析盘点:银行业低代码应用需要规避两大误区
【时序】TFT:具有可解释性的时间序列多步直接预测 Transformers[学习中...,亟待解决]