我是靠谱客的博主 老迟到鞋垫,这篇文章主要介绍c++实现while循环语句,生成随机数,猜数字游戏,现在分享给大家,希望可以做个参考。

while循环语句

在屏幕中打印0~9这十个数字

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream> using namespace std; int main() { /* while循环语句:满足循环条件,执行循环语句 语法:while(循环条件){循环语句} 只要循环条件的结果为真,就执行循环语句 */ //在屏幕中打印0~9这十个数字 int num = 0; while (num <= 9) { cout << num << endl; num++; } system("pause"); return 0; }

生成随机数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream> using namespace std; int main() { //猜数字:系统随机生成一个1-100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果才对恭喜玩家胜利,并且退出游戏 int num = rand() % 100 + 1;//系统随机生成一个1-100之间的数字 cout << num << endl; int num1 = rand() % 100 + 1;//系统随机生成一个1-100之间的数字 cout << num1 << endl; int num2 = rand() % 100 + 1;//系统随机生成一个1-100之间的数字 cout << num2 << endl; system("pause"); return 0; }

生成随机数截图

猜数字

:系统随机生成一个1-100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果才对恭喜玩家胜利,并且退出游戏。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream> using namespace std; int main() { //猜数字:系统随机生成一个1-100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果才对恭喜玩家胜利,并且退出游戏 int num = rand() % 100 + 1;//系统随机生成一个1-100之间的数字 int a = 0; cout << "请输入一个数字" << endl; cin >> a; while (a != num) { if (a > num) { cout << "您输入的数字偏大,请重新输入:" << endl; cin >> a; } else if (a < num) { cout << "您输入的数字偏小,请重新输入:" << endl; cin >> a; } } cout << "恭喜你猜对了!" << endl; system("pause"); return 0; }

在这里插入图片描述
但是,此代码优缺点,每次生成的随机数都是42。
为了解决这个问题,要添加一个随机数种子,是利用当前系统时间生成随机数,防止每次随机数都一样。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream> #include<ctime>//随机数头文件 using namespace std; int main() { //猜数字:系统随机生成一个1-100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果才对恭喜玩家胜利,并且退出游戏 srand((unsigned int)time(NULL));//添加随机数种子,作用是利用当前时间生成随机数,防止每次随机数都一样 int num = rand() % 100 + 1;//系统随机生成一个1-100之间的数字 int a = 0; cout << "请输入一个数字" << endl; cin >> a; while (a != num) { if (a > num) { cout << "您输入的数字偏大,请重新输入:" << endl; cin >> a; } else if (a < num) { cout << "您输入的数字偏小,请重新输入:" << endl; cin >> a; } } cout << "恭喜你猜对了!" << endl; system("pause"); return 0; }

改进生成随机数部分

改进猜数字游戏:

添加游戏次数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream> #include<ctime>//随机数头文件 using namespace std; int main() { //猜数字:系统随机生成一个1-100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果才对恭喜玩家胜利,并且退出游戏 srand((unsigned int)time(NULL));//添加随机数种子,作用是利用当前时间生成随机数,防止每次随机数都一样 int num = rand() % 100 + 1;//系统随机生成一个1-100之间的数字 int a,count = 0; //int count = 0; cout << "请注意:本次游戏共5次机会!" << endl; cout << "请输入一个数字" << endl; cin >> a; while (a != num) { if (a > num && count < 4) { cout << "您输入的数字偏大,请重新输入:" << endl; cin >> a; count = count + 1; } else if (a < num && count < 4) { cout << "您输入的数字偏小,请重新输入:" << endl; cin >> a; count = count + 1; } else { cout << "本次游戏五次机会已经用完,游戏失败! " << endl; break; } } if (a == num) { cout << "恭喜你猜对了!" << endl; } system("pause"); return 0; }

改进游戏次数

最后

以上就是老迟到鞋垫最近收集整理的关于c++实现while循环语句,生成随机数,猜数字游戏的全部内容,更多相关c++实现while循环语句内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部