使用for迴圈列印三角形、菱形、九九乘法表
阿新 • • 發佈:2020-11-13
使用For迴圈列印三角形、菱形、九九乘法表
使用for迴圈列印三角形
package com.zjl.struct; public class ForDemo05 { public static void main(String[] args) { //列印三角形 for (int i = 1; i <=5; i++) { for(int k=5;k>i;k--){ System.out.print(" "); } for (int j=1;j<=2*i-1;j++){ System.out.print("*"); } System.out.println(""); } } }
使用for迴圈列印菱形
package com.zjl.struct; public class ForDemo04 { //列印菱形 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<=2*i-1;j++){ System.out.print("*"); } System.out.println(); } for(int i=1;i<5;i++){ for(int k=1;k<=i;k++){ System.out.print(" "); } for (int j=7;j>=2*i-1;j--){ System.out.print("*"); } System.out.println(); } } }
使用for迴圈列印九九乘法表
package com.zjl.struct; public class ForDemo03 { //列印九九乘法表 public static void main(String[] args) { for (int i = 1; i <=9; i++) { for(int j=1;j<=i;j++){ System.out.print(j+"*"+i+"="+i*j+" "); } System.out.println(); } } }
重點:多層for迴圈巢狀