当前位置: 首页 > news >正文

循环结构(个人学习笔记黑马学习)

while循环语句

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

#include <iostream>
using namespace std;int main() {int i = 0;while (i < 10) {cout << i << endl;i++;}system("pause");return 0;
}

 

练习案例: 猜数字


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

#include <iostream>
using namespace std;
#include <ctime>int main() {srand((unsigned int)time(NULL));int num = rand() % 100 + 1;int num2 = 0;cout << "请输入数字" << endl;while (1) {cin >> num2;if (num > num2) {cout << "猜小了" << endl;}else if (num < num2) {cout << "猜大了" << endl;}else {cout << "猜对了" << endl;break;}}system("pause");return 0;
}

 

 


do……while循环语句 

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

#include <iostream>
using namespace std;int main() {int num = 0;do {cout << num << endl;num++;} while (num < 10);system("pause");return 0;
}

 


练习案例: 水仙花数


案例描述: 水仙花数是指一个 3 位数,它的每个位上的数字的 3次幕之和等于它本身
例如: 1^3 + 5^3+ 3^3 = 153
请利用do...while语句,求出所有3位数中的水仙花数 

#include <iostream>
using namespace std;int main() {int i = 100;do {int x1=i / 100;int x2 = i / 10 % 10;int x3=i % 10;if (x1 * x1 * x1 + x2 * x2 * x2 + x3 * x3 * x3 == i) {cout << i << endl;}i++;} while (i < 1000);system("pause");return 0;
}

 

for循环 

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

#include <iostream>
using namespace std;int main() {for (int i = 0; i < 10; i++) {cout << i << endl;}system("pause");return 0;
}

 

 


练习案例:敲桌子


案例描述:从1开始数到数字100如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。

#include <iostream>
using namespace std;int main() {for (int i = 0; i <= 100; i++) {if (i % 10 == 7 || i / 10 == 7 || i % 7 == 0) {cout << "拍桌子" << endl;}else {cout << i << endl;}}system("pause");return 0;
}

 

 


嵌套循环

打印10*10方正

#include <iostream>
using namespace std;int main() {for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {cout << "* ";}cout << endl;}system("pause");return 0;
}

 


乘法口诀表 

#include <iostream>
using namespace std;int main() {for (int i = 1; i <= 9; i++) {for (int j = 1; j <= i; j++) {cout << j << "*" << i <<"=" << i * j<<"  ";}cout << endl;}system("pause");return 0;
}

 

http://www.lryc.cn/news/145476.html

相关文章:

  • ceph中PGLog处理流程
  • macOS使用命令行连接Oracle(SQL*Plus)
  • Mac下使用Homebrew安装MySQL5.7
  • centos安装Nginx配置Nginx
  • Linux环境下搭建使用缓存中间件Redis
  • Oracle 本地客户端连接远程 Oracle 服务端并使用 c# 连接测试
  • java中上传文件先下载到本地再上传还有就是直接通过文件流url地址进行上传优缺点?
  • 华为复合vlan(mux vlan)
  • 第62步 深度学习图像识别:多分类建模(Pytorch)
  • GPT带我学-设计模式-适配器模式
  • Pyecharts教程(七):使用pyecharts创建堆叠柱状图的示例
  • C++中的强制转换的常用类型及应用场景详解
  • ubuntu调整时区
  • mybatis:动态sql【2】+转义符+缓存
  • 2021年09月 C/C++(五级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • Ansible学习笔记1
  • 解决centos离线安装cmake找不到OpenSSL问题
  • Java 中数据结构ArrayList的用法
  • UDP 多播(组播)
  • 分布式环境集成JWT(Java Web Token)
  • Python实战之数据表提取和下载自动化
  • Midjourney学习(三)6个高级应用
  • C语言:指针类型的意义
  • 如何将 PDF 转换为 Word:前 5 个应用程序
  • AP5192 DC-DC降压恒流LED汽车头灯摩托车电动车大灯电源驱动
  • Python Opencv实践 - Canny边缘检测
  • Python编程练习与解答 练习119:低于和高于平均水平
  • vue中的nextTick的作用
  • 如何通过四个步骤清理网络防火墙规则
  • 打开谷歌浏览器远程调试功能