当前位置:网站首页>小试牛刀—猜数字游戏
小试牛刀—猜数字游戏
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,今天想说的就这么多啦,以后再见吧。
边栏推荐
- Data Persistence Technology - MP
- 如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
- WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)
- 建情人节表白网站(超详细过程,包教包会)
- 科学论文和学术论文写作
- Comparison of ipv4 and ipv6 (IPV4)
- Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
- CWE4.8 -- 2022年危害最大的25种软件安全问题
- Is the working process of the belt you know the story - actionreducerstore
- 列表页优化思路
猜你喜欢
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
Different lower_case_table_names settings for server ('1') and data dictionary ('0') solution
cesium-Web网页优化进阶
一周精彩内容分享(第14期)
Chrome开发自定义右键菜单实现快速跳转到指定页面
Spark GC日志分析
St. Regis Takeaway Project: New dishes and dishes paged query
Use docker to build mysql master-slave
file contains vulnerabilities
DCM middleware family welcomes a new member
随机推荐
Use jOOQ to write vendor-agnostic SQL with JPA's native query or @Formula.
IDEA configure method annotation automatic parameters
JVS轻应用的组成与配置
Addition logic for SAP Commerce Cloud Product Review
Three-Phase PWM Rectifier Predictive Direct Power Control
学习笔记 Golang 写入文件(io.WriteString、ioutil.WriteFile、file.Write、write.WriteString)
Exploring Plain Vision Transformer Backbones for Object Detection Paper Reading Notes
最长算术(暑假每日一题 11)
Candence学习篇(11) allegro中设置规则,布局,走线,铺铜
MySQL limit paging query and performance issues
消息队列面试题(2022最新整理)
机器学习基本概念
Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
ipv4和ipv6对比(IPV4)
Shengxin Weekly Issue 38
JVS设置不同应用的登录时效时间
第十二章 使用中的 OpenAPI 属性
After Effects 教程,如何在 After Effects 中修复曝光不足的镜头?
关于==和equals的区别和联系,面试这么回答就可以
mysql根据多字段分组——group by带两个或多个参数