要求:打印出一个菱形的字母,从字母A開始.
代码:
package com.huawei.mianshi; public class Demo2 { private static int LINE = 4; public static void main(String[] args) { run(); } private static void run() { //从大写字母A開始 char A = 'A'; //循环的次数i=1代表第一行 for (int i = 1; i <= LINE; i++) { //打印空格 for (int k = 1; k <= (LINE - i); k++) System.out.print(" "); //打印字母 for (int j = 0; j < i; j++) { System.out.print(A++); System.out.print(" "); } //换行 System.out.println(); } //逆向打印 for (int i = LINE; i >= 1; i--) { //打印空格 for (int k = 1; k <= (LINE - i); k++) System.out.print(" "); //打印字母 for (int j = 0; j < i; j++) { System.out.print(A++); System.out.print(" "); } //换行 System.out.println(); } } }