#include <iostream>
using namespace std;
int main(){
//满足循环条件,执行循环语句
//语法:
//while(循环条件){循环语句}
//只要循环条件的结果为真,就执行循环语句
//打印0~9这10个数字
int num = 0;
cout << num << endl;//0
num++;
cout << num << endl;//1
while(num<10){
cout << num << endl;
num ++ ;
}
/*
输出结果:
1
2
3
4
5
6
7
8
9
*/
}
#include<stdlib.h> //导入随机模块rand()
#include <iostream>
//time系统头文件
#include<ctime>
using namespace std;
int main(){
/*
C语言/C++里没有自带的random(int number)函数。
(1) 如果你只要产生随机数而不需要设定范围的话,你只要用rand()
就可以了:rand()会返回一随机数值, 范围在0至最大随机数。
rand()保存在#include<stdlib.h>的头文件
) 如果你要随机生成一个在一定范围的数,你可以在宏定义中定义一
个random(int number)函数,然后在main()里面直接调用random()函数
:
例如:随机生成10个0~100的数:
#include<iostream>
#include<stdlib.h>
using namespace std;
#define random(x) (rand()%x)
int main()
{
for(int x=0;x<10;x++)
cout<<random(100)<<endl;
return 0;
}
(3)但是上面两个例子所生成的随机数都只能是一次性的,如果你第二次
运行的时候输出结果仍和第一次一样。这与srand()函数有关。srand()
用来设置rand()产生随机数时的随机数种子。在调用rand()函数产生随
机数前,必须先利用srand()设好随机数种子(seed), 如果未设随机数种子,
rand()在调用时会自动设随机数种子为1。上面的两个例子就是因为没有设置
随机数种子,每次随机数种子都自动设成相同值1 ,
进而导致rand()所产生的随机数值都一样。
*/
//添加随机数种子,利用当前系统时间生成随机数,防止
//每次随机数都一样
srand((unsigned int)time(NULL)) ;
//猜数字 系统随机生成1~100之间的随机数字
//1、系统生成随机数
int num = rand()%100+1; //rand()%100生成0~99的随机数
cout << num << endl;
//2、玩家进行猜测
int val = 0;//玩家输入的数据
while(1){
cout <<"请输入:" << endl;
cin >> val;
//3、判断比较
// 对
// 错 猜大或者小 重新猜测
if(val>num){
cout << "猜测过大" << endl;
}else if(val<num){
cout << "猜测过小" << endl;
}else{
cout << "猜对了" << endl;
//猜对退出游戏
break;//打破整个循环
}
}
}
#include <iostream>
using namespace std;
int main(){
/*
do..while
与while的区别在于do...while会先执行一次,再判断循环条件
*/
int num = 0;
do{
cout << num << endl;
num++;
} while(num<10);
/*
输出结果:
0
1
2
3
4
5
6
7
8
9
*/
}
#include <iostream>
using namespace std;
int main(){
/*
水仙花数
是指一个3位数,它的每个位上的数字的3次幂之和等于它本身
例如:
1^3 + 5^3 + 3^3 = 153
*/
//1、将所有的三位数进行输出(100~999)
int num = 100;
do{
//如果是水仙花数,就打印
int a = 0;//个位
int b = 0;//十位
int c = 0; //百位
a = num %10;
b = num/10%10;
c = num/100;
if(a*a*a + b*b*b + c*c*c == num){
cout <<num << endl;
}
num ++ ;
} while(num<1000);
//2、在所有 三位数中找到水仙花数
// 水仙花数
// 获取个位
// 获取十位
// 获取百位
// 判断
// 获取个位:对数取模于10 153%10=3
//获取十位:对数整除于10 153/10=15 再对商取模于10 15%10=5
//获取百位:对100进行整除 153/100=1 直接整除100获取百位
/*
153
370
371
407
*/
}
#include<iostream>
using namespace std;
int main(){
/*
for循环语句:
满足循环条件,执行循环语句
for(起始表达式;条件表达式;末尾循环体){循环语句;}
*/
for(int i=0;i<10;i++){
cout << i << endl;
}
/*
输出结果:
0
1
2
3
4
5
6
7
8
9
*/
}
#include<iostream>
using namespace std;
int main(){
/*
敲桌子:
从1开始数到100,如果遇到7或者数字中含7,或者是7的倍数,
就敲桌子
*/
//1、先输出1~100
//2、从数字中找到符合条件的特殊数字
//7的倍数7 14 21 28... %7==0
//个位有7 7 17 27 37... %10==7
//十位有7 70 71 72... /10==7
for(int i= 1;i<=100;i++){
if(i%7==0 || i%10==7 || i/10==7){
cout << i << endl;
}
}
/*
输出结果:
7
14
17
21
27
28
35
37
42
47
49
56
57
63
67
70
71
72
73
74
75
76
77
78
79
84
87
91
97
98
*/
}
#include<iostream>
using namespace std;
int main(){
/*
嵌套循环
*/
//外层循环 外层执行一次 内层执行一周
for(int i=0;i<10;i++){
//内层循环
for(int j = 0;j<10;j++){
cout << "* ";
}
cout << endl;
}
/*
输出结果:
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
*/
}
#include<iostream>
using namespace std;
int main(){
/*
九九乘法口诀表:
列数 * 函数 = 计算结果
列数<=当前的函数
*/
//打印行数
for(int i = 1;i<=9;i++){
//cout << i << endl;
//打印列
for(int j = 1;j<=i;j++){
cout<< j<<"*"<< i <<" = "<<i*j<<" ";
}
cout<< endl;
}
/*
输出结果:
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16
1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25
1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36
1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49
1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 48 7*8 = 56 8*8 = 64
1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81
*/
}
#include<iostream>
using namespace std;
int main(){
/*
跳转语句
break
1、出现在switch条件语句中,作用是终止case并跳出switch
2、出现在循环语句中,作用是跳出当前的循环语句
3、出现在嵌套循环中,跳出最近的内层循环语句
*/
//1、 出现在switch条件语句中
cout << "请选择游戏难度:" << endl;
cout << "1、普通:" << endl;
cout << "2、中等:" << endl;
cout << "3、困难:" << endl;
int select = 0;//创建变量存储选择的结果
cin >> select;
switch(select){
case 1:
cout << "你选择的是普通难度:" << endl;
break;
case 2:
cout << "你选择的是中等难度:" << endl;
break;
case 3:
cout << "你选择的是困难难度:" << endl;
break;
default:
break;
}
/*
输出结果:
请选择游戏难度:
1、普通:
2、中等:
3、困难:
2
你选择的是中等难度:
*/
//2、出现在循环语句中
//打印到6就推出循环
for(int i=0;i<10;i++){
if(i==6){
break;
}
cout << i << endl;
}
/*
输出结果:
0
1
2
3
4
5
*/
//3、出现在嵌套循环中
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(j==6){
break;
}
cout << "*";
}
cout << endl;
}
/*
输出结果:
******
******
******
******
******
******
******
******
******
******
*/
}
#include<iostream>
using namespace std;
int main(){
/*
continue语句:
跳出本次循环中尚未执行的语句,继续下次循环
*/
for(int i=0;i<=10;i++){
//如果是奇数输出,偶数不输出
if(i%2==0){
continue;
}
cout << i << endl;
}
/*
输出结果:
1
3
5
7
9
*/
}
#include<iostream>
using namespace std;
int main(){
/*
goto语句:
可以无条件跳转语句
如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
*/
cout << "1" << endl;
goto FLAG;//标记的名称一般大写
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
FLAG://跳过其他行,直接执行这段代码 跳转太灵活,不推荐使用
//跳来跳去,影响整个代码的结构
cout << "5" << endl;
/*
输出结果:
1
5
*/
}