1 package com.situ.chapter; 2 3 import java.util.Scanner; 4 5 //1. 从控制台输入一个三角形高度,拼成一个菱形。 6 public class Subject02 { 7 public static void main(String[] args) { 8 System.out.println("请输入三角形的高度:"); 9 Scanner scanner = new Scanner(System.in); 10 int height = scanner.nextInt(); 11 int i = 0; 12 while (i < height) { 13 int j = 0; 14 while (j < height - i - 1) { 15 System.out.print(" "); 16 j++; 17 } 18 // 输出星号 19 int k = 0; 20 while (k < 2 * i + 1) { 21 System.out.print("*"); 22 k++; 23 } 24 System.out.println(); 25 i++; 26 } 27 28 // 打印下半部分三角形 29 // ***** 1 1 5 30 // *** 2 2 3 31 // * 3 3 1 32 int m = 1; 33 while (m <= height - 1) { 34 int n = 1; 35 while (n <= m) { 36 System.out.print(" "); 37 n++; 38 } 39 int l = 0; 40 while (l <= 2 * (height - 1 - m)) { 41 System.out.print("*"); 42 l++; 43 } 44 m++; 45 System.out.println(); 46 } 47 scanner.close(); 48 } 49 }