输出棱形

    public static void main(String[] args) {
        int i, j;
        Scanner scan = new Scanner(System.in);
        String c = scan.next();
        int h = 5;
        int r = h / 2 + 1;

        for (i = 1; i <= h; i++) {
            if (i <= r) {
                for (j = 0; j < (r - i); j++) System.out.printf(" ");
                for (j = 0; j < (2 * i - 1); j++) System.out.printf(c);

            } else {
                for (j = 0; j < (i - r); j++) System.out.printf(" ");
                for (j = 0; j < (h - (i - r) * 2); j++) System.out.printf(c);
            }
            System.out.printf("\n");
        }
    }

输出三角形

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int i, j;
        Scanner scan = new Scanner(System.in);
        String c = scan.next();
        int h = 3;
        int r = h / 2 + 1;
        for (i = 1; i <= h; i++) {
            for (j = 0; j <= (r - i); j++) System.out.printf(" ");
            for (j = 0; j < (2 * i - 1); j++) System.out.printf(c);
            System.out.printf("\n");
        }
    }
}