当前位置:网站首页>小试牛刀—猜数字游戏
小试牛刀—猜数字游戏
2022-07-31 12:10:00 【林深方见鹿】
咱今天就通过这个简单的猜数字游戏来深入的再认识一下选择结构和循环结构。
这个猜数字游戏大概就是说系统随机产生一个1~100之间的数字,然后来猜猜这个数是谁。好了,让我们来看一下代码:
//VS2017环境下编译通过
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
int select = 1;
while (select)
{
printf("********************************\n");
printf("*****猜 数 字 游 戏*****\n");
printf("********************************\n");
printf("*----------[1] Play-------------\n");
printf("*----------[0] Exit-------------\n");
printf("请选择:>\n");
scanf("%d", &select);
if (select == 0)
{
break;//A行
//跳出本次循环,如果不加,程序会接着往下执行
}
if (select != 1)
{
printf("选择有误,请重新选择:>\n");
continue;//A+
}
int random = rand() % 100 + 1;//B
int guess = 0;//C
while (1)
{
//位置D
printf("请猜测并且输入一个1~100之间的整数:>\n");
scanf("%d", &guess);
if (guess == random)
{
printf("恭喜你,猜对啦!\n");
break;
}
if (guess < random)
{
printf("猜小啦,再猜一次!:>\n");
}
if (guess > random)
{
printf("猜大啦,再猜一次!:>\n");
}
}
}
printf("欢迎下次再来!\n");
return 0;
}
让我来对程序中的A行做出一些解释:
1、break是跳出整个循环的意思,也就是说,当用户选择了0,表明希望退出时,我们将让用户退出我们的游戏,来看一下运行结果:
还有一个关键字continue,意识是跳出此次循环的意思,我们来看一下A+行的continue的效果:
可以看出来,continue跳出了本次的循环,但是while循环还是在进行中,以后用这两个关键字的时候一定要根据使用场景分清楚该让哪个关键字上场。
2、关于B行和C行代码位置对程序运行结果的影响
当把B行和C行代码放到位置D时,悲剧就发生了……
看一下悲剧到底是什么样子的:
不难看出,逻辑出问题了,每次进入第二个while循环,那么就会重新生成一个随机数,这样子也就永远不能猜对了。所以,变量如何放置是至关重要的,要充分考虑变量的作用域。
3、窗口美化问题
不难看出,上面的窗口是有一点点丑的吧…
我们来简单的给它上个色,设置一下大小,稍微的美观一点
在程序中加上几行代码:
system("title 猜数字游戏");
system("mode con cols=35 lines=13");
system("color AE");
看一下效果,不过好像也没有好看到哪里去……我尽力了,调色什么的,以后再说吧哈哈哈哈哈
OK,今天想说的就这么多啦,以后再见吧。
边栏推荐
- 一周精彩内容分享(第14期)
- Banyan Tree Loan GPU Hardware Architecture
- 【Shader】Shader官方示例[通俗易懂]
- WPF中TabControl动态获取当前选中的TabItem
- Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
- kernel syscore
- Use Excel to read data exposed by SAP ABAP CDS View through ODBC
- 列表页优化思路
- mysql根据多字段分组——group by带两个或多个参数
- Wearing detection and action recognition of protective gear based on pose estimation
猜你喜欢
SAP ABAP OData 服务如何支持 $filter (过滤)操作试读版
chroot命令
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
imx6ull看门狗使用
MySQL row-level locks (row locks, adjacent key locks, gap locks)
Acwing第 62 场周赛【未完结】
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
Docker build Mysql master-slave replication
Full GC (Ergonomics)排查分析
Distributed id solution
随机推荐
Distributed id solution
Mysql环境变量的配置(详细图解)
0x80070570文件或目录损坏且无法删除(0x80070091怎么删除)
Cognitive-exercise rehabilitation medical robot application design
Use docker to build mysql master-slave
JVS设置不同应用的登录时效时间
Json和对象之间转换的封装(Gson)
Basic use of dosbox [easy to understand]
三相PWM整流器预测直接功率控制
In Excel using ODBC consumer SAP ABAP CDS view
生信周刊第38期
立方体IV(暑假每日一题 10)
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
Redis学习笔记-3.慢查询和其他高级数据结构
Power BI----几个常用的分析方法和相适应的视觉对象
Initial JDBC programming
502 bad gateway causes and solutions
栈和队列的基本概念
SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
多线程学习笔记-2.final关键字和不变性