一、打印效果
二、实现代码汇总
为了方便初学者对代码的理解,建议熟练t1到t5为各种三角的打印,然后再进行菱形的打印实现。
package circulationDemo; import java.util.Scanner; public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要打印的行数:");
int num = input.nextInt();
t1(num);// 正直角
split();
t2(num);// 倒直角
split();
t3(num);// 等腰三角
split();
t4(num);// 等腰倒三角++
split();
t5(num);// 等腰倒三角--
split();
t6(num);// 实心菱形
split();
t7(num);// 空心菱形(1)
split();
t8(num);// 空心菱形(2)
split();
t9(num);// 空心菱形(3)
} private static void t9(int num) {
for (int k = 1; k <= num; k++) {
for (int i = 1; i <= (num - k); i++) {
System.out.print(" ");
}
System.out.print("*");
for (int i = 1; i <= ((k - 2) * 2 + 1); i++) {
System.out.print(" ");
}
if (k != 1) {
System.out.print("*");
}
System.out.println();
}
for (int k = num-1; k >= 1; k--) {
for (int i = 1; i <= (num - k); i++) {
System.out.print(" ");
}
System.out.print("*");
for (int i = 1; i <= ((k - 2) * 2 + 1); i++) {
System.out.print(" ");
}
if (k != 1) {
System.out.print("*");
}
System.out.println();
}
} private static void t8(int num) {
// 上半部分
for (int i = 1; i <= num; i++) {
// 打印上半部分空格
for (int j = 1; j <= num - i; j++) {
System.out.print(" ");
}
// 打印原本实心的*部分
for (int j = 1; j <= 2 * i - 1; j++) {
// 仅在一行的开头和末尾打印*
if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// 下半部分
for (int i = 1; i <= num - 1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (num - i - 1) + 1; j++) {
if (j == 1 || j == 2 * (num - i - 1) + 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
} } private static void t7(int num) {
// 打印上半部分
for (int i = 1; i <= num; i++) {
// 打印空格
for (int j = 1; j <= (num - i); j++) {
System.out.print(" ");
}
// 打印*的个数
if (i == 1) {
System.out.print("*");
} else {
System.out.print("*");
// 打印菱形里的空格
for (int j = 1; j <= (2 * i - 3); j++) {
System.out.print(" ");
}
System.out.print("*");
} System.out.println();
}
// 打印下半部分
for (int i = num - 1; i > 0; i--) {
// 打印空格
for (int j = 1; j <= (num - i); j++) {
System.out.print(" ");
}
// 打印*的个数
if (i == 1) {
System.out.print("*");
} else {
System.out.print("*");
// 打印菱形里的空格
for (int j = 1; j <= (2 * i - 3); j++) {
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
} private static void t6(int num) {
// 上半部分
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// 下半部分
for (int i = 1; i <= num - 1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (num - 1 - i) + 1; j++) {
System.out.print("*");
}
System.out.println();
}
} private static void t5(int num) {
for (int i = num; i > 0; i--) {
for (int j = 1; j <= num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
} private static void t4(int num) {
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i - 1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (num - i) + 1; j++) {
System.out.print("*");
}
System.out.println();
}
} private static void t3(int num) {
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
} private static void split() {
System.out.println("---------------");
} private static void t2(int num) {
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num - i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
} private static void t1(int num) {
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
} }