For the use of double loop
Double loop of for
Syntax:
for (loop condition 1) { loop operation 1; for (loop condition 2) { loop operation 2 } }
The for loop of loop condition 1 can be called the outer loop, and the loop of loop condition 2 can be called the inner loop.
Example display:
1. Rectangular
code display:
package test;
//长方形
public class test02 {
public static void main(String[] args) {
for (int i = 1; i <= 5 ; i++) {
for (int j = 1; j <=5 ; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Running results:
2. Parallelogram
Code display:
package test;
//平行四边形
public class test03 {
public static void main(String[] args) {
for (int i = 1; i <=5 ; i++) {
for (int k = 1; k <=5-i ; k++) {
System.out.print(" ");
}
for (int j = 1; j <=5 ; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Running result:
3, triangle
code display:
package test;
//三角形
public class test04 {
public static void main(String[] args) {
for (int i = 1; i <= 5 ; i++) {
for (int j = 1; j <= 5-i; j++) {
System.out.print(" ");
}
for (int k =1; k <=2*i-1 ; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Running result:
4, inverted triangle
Code display:
package zuoye;
public class t01 {
public static void main(String[] args) {
for (int i = 1; i <=5 ; i++) {
for (int k = 1; k <= i-1 ; k++) {
System.out.print(" ");
}
for (int j = 5; j >=i ; j--) {
System.out.print("* ");
}
System.out.println(" ");
}
}
}
operation result: