for 循环 大圈套小圈

#1.1for循环思想

1
2
3
4
5
6
7
8
9
public class code4 {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
System.out.println("ok");
}
}
}
}

image-20181215084305163


读第一个for循环初始化一个x 变量x=0:

​ 先执行x=0; 执行x<3满足条件—>

执行第二个for循环语句初始化一个变量y=0;

​ 先执行y=0;执行y<4满足条件–>打印一次ok

当内循环没有结束时循环出不去执行–>y++;打印四次ok;

y++=4时候y<4不成立 跳出循环 y在内存中消失 –>执行–x++

满足条件 又产生一个y=0,内循环继续循环四次 所以一共打印12个ok


image-20181215084525009


#1.2验证doc命令行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class code5 {
/**
* for循环基本思想demo1
* 输出:
* *****
* *****
* *****
* *****
*/
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

image-20181215085157010

i 控制行 第一个for循环

j控制列 第二个for循环 (每一列的个数)

#1.3验证doc命令行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class code6 {
/** 输出doc界面:
* *****
* ****
* ***
* **
* *
*/
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}

}
}

image-20181215090711158

分析:

image-20181215090844549

image-20181215090906622

image-20181215090927631


#1.4验证doc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class code7 {
/**
* *
* **
* ***
* ****
* *****
*/

public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

image-20181215092601659

image-20181215092622770


#1.5验证doc⭐️

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class code8 {
/**
* 54321
* 5432
* 543
* 54
* 5
*/
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 5; j > i; j--) {
System.out.print(j);
}
System.out.println();
}
}
}

image-20181215094758361

image-20181215095339271

int j=5 j>i j– 其中j—是关键


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

public class code9 {
/**
* 1
* 22
* 333
* 4444
* 55555
*/
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}

image-20181215100813415

image-20181215100923906


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

public class code10 {
/**
*
* * * * * *
* -* * * *
* --* * *
* ---* *
* ----*
* 由两个图形组成一个由向下的*组成
* 一个由向上的——组成
*/

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int z = i; z<=5 ; z++) {
System.out.print("* ");
}System.out.println();
}

}
}

image-20181215103926561

分析:

image-20181215103952180


#1.6作 业:打印

image-20181215104212818

image-20181215104231193


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

public class code8 {
/**
* 54321
* 5432
* 543
* 54
* 5
*/
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 5; j >i; j--) {
System.out.print(j);
}System.out.println();
}

}
}

分析:

image-20181215105144545

image-20181215105317814

规律:for内循环 如果j<i 不成立 改成j>i 并且让j–




image-20181215105815854

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

public class code9 {
/**
* 1
* 22
* 333
* 4444
* 55555
*/
public static void main(String[] args) {
for (int i = 1; i <=5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i);
}System.out.println();
}
}
}