利用嵌套打印十行 *

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;

int main()
{
//利用嵌套打印十行*
for (int j = 0; j <= 5; j++)
{
for (int i = 0; i <= 10; i++)
{
cout << "*";

}
cout << endl;
}
return 0;
}


利用嵌套打印乘法口诀表

首先打印行

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 9; i++)
{
cout << "i" << endl;
}
system("pause");
return 0;
}

运行此代码发现打印的是九行i

image-20220111001728129

原因是i加了 “”

将 “” 去掉

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 9; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}

嵌套for循环拆解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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;
}

}
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
#include<iostream>
using namespace std;

int main()
{
int i = 1;
for (; ; )
{
if (i > 9)
{
break;
}
cout << i << endl;

int j = 1;
for (; ; )
{
if (j > i)
{
break;
}
cout << j;
j++;

}
i++;
}
system("pause");
return 0;
}

打印列数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 9; i++)
{

for (int j = 1; j <= i; j++)
{
cout << "j*i"<<"="<<"\t";

}
cout << endl;
}
system("pause");
return 0;
}

发现打印完后是这样的结果

image-20220111130021215

改正如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 9; i++)
{

for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << j * i << "\t";
}
cout << endl;
}
system("pause");
return 0;
}