- 首先打印三角形左边空白
- 其次打印中间左半边的三角形
- 最后打印右半边的三角形
代码如下:
package com.xiaoye.scanner;
import java.util.Scanner;
public class 三角形 {
public static void main(String[] args) {
// 打印三角形
System.out.println("请输入你要打印三角形的行数:");
Scanner scanner = new Scanner(System.in);
int num = Integer.parseInt(scanner.next());
for (int i = 0; i < num; i++){
for (int j = num; j > i + 1; j--)
System.out.print(' ');
for (int j = 0; j <= i; j++){
System.out.print("*");
}
for (int j = 0; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
输出结果:
—————————————————
————————
——



