• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏吧

双重for循环的实例（时钟运转、长方形、平行四边形、三角形）

2周前 (11-21) 8次浏览

1、双重for循环模拟时钟运转

``````public class forDouble {
public static void main(String[] args) {
for (int i = 1; i <13 ; i++) {
System.out.println("现在是"+i+"点");
for (int j = 1; j <60 ; j++) {
System.out.println("现在是"+i+"点"+j+"分");
}
System.out.println("--------");
}
}
}
``````

2、双重for循环打印长方形

``````public class Fang {
public static void main(String[] args) {
for (int i = 1; i <6 ; i++) {
for (int j = 1; j <6 ; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
``````

3、双重for循环打印平行四边形

``````public class Fang1 {
public static void main(String[] args) {
for (int i = 1; i <6; i++) {
for (int k = 1; k <=5-i ; k++) {
System.out.print("  ");
}
for (int j = 1; j <6 ; j++) {
System.out.print("* ");
}
System.out.println("");
}
}
}
``````

4、双重for循环打印等腰三角形

``````public class Fang2 {
public static void main(String[] args) {
for (int i = 1; i <6 ; i++) {
for (int j = 0; j <5-i ; j++) {
System.out.print("  ");
}
for (int k = 1; k <=2*i-1 ; k++) {
System.out.print("* ");
}
System.out.println(" ");
}
}
}
``````