JAVA 控制檯列印三角形
package inputTriangle;
import java.util.Scanner;
public class riangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("請輸入要列印三角形的行數:");
// 輸直角三角形
int line = input.nextInt();
for (int i = 0; i <= line; i++) {
for (int j = 0; j < i; j++) {
System.out.print("* ");
}
System.out.println("");
}
// 輸出倒直角三角形
System.out.println("---------列印倒直角三角形-----------");
for (int i = 0; i <= line; i++) {
for (int j = line; j > i; j--) {
System.out.print("* ");
}
System.out.println("");
}
// 輸出等腰三角形
System.out.println("---------列印等腰三角形-----------");
for (int i = 0; i <= line; i++) {
for (int j = line; j > i; j--) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("* ");
}
System.out.println("");
}
}
}