用IDEA列印一個菱形
阿新 • • 發佈:2021-11-13
package com.tu.struct;
//整個程式打印出
/*
*
***
*****
*******
*********
*******
*****
***
*
*/
public class TestDemo {
public static void main(String[] args) {
//列印三角形 5行
/*
*
***
*****
*******
*********
*/
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j <i; j++) {
System.out.print("*");
}
System.out.println();
}
//下面的語句打印出
/*
*******
*****
***
*
*/
for (int m = 1; m <= 5; m++) {
for (int n = 1; n<=m; n++) {
System.out.print(" ");
}
for (int n = 4; n >= m; n--) {
System.out.print("*");
}
for (int n = 4; n > m; n--) {
System.out.print("*");
}
System.out.println();
}
}
}