2.5重学C++之【嵌套循环、跳转语句break】
#include<iostream>
using namespace std;
int main(){
//2 循环结构
//2-4 嵌套循环
for (int i=0; i<10; i++){
for (int j=0; j<10; j++){
cout << "* "; //此处不要<< endl,不然**间会换行
}
cout << endl; //即为换行
}
//案例-乘法口诀表
//列数*行数
//每一行:列数=当前行数 --> 列数<=当前行数
for (int i=1; i<=9; i++){
//cout << i << endl;
//for (int j=1; j<=9; j++){
for (int j=1; j<=i; j++){
//cout << j;
cout << j << "*" << i << "=" << i*j << " ";
}
cout << endl;
}
//3 跳转语句
//3-1 break
//(1)在switch条件语句中,终止case并跳出switch
//(2)循环语句中,跳出当前循环不再执行
//(3)嵌套循环中,跳出最近的内层循环
//示例1
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;
default:
cout << "进入困难副本。。。" << endl;
break;
}
//示例2
for (int i=1; i<10; i++){
if (i==5){
break;
}
cout << i << endl;
}
//示例3
for (int i=0; i<10; i++){
for (int j=0; j<10; j++){
if (j==5){ //10*5的"*"矩阵
break;
}
cout << "*";
}
cout << endl;
}
return 0;
}