程序如下:
/*
打印出
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
要求打印的行数大于3行,且行数为奇数
*/
import java.util.Scanner;
class Demo1 {
public static void main(String[] args){
System.out.println("Please input to print the number of rows");
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
if(rows <3 || rows % 2 == 0) {
System.out.println("Please enter the odd number greater than 3");
return ;
}
char ch = 'A';
int n = 0;
for(int i = 0;i < rows; i++){
n = i;
ch = 'A';
if(i > (rows - 1) / 2){
n = rows - 1 - i;
}
for(int x = 0;x < (rows - (2 * n + 1)) / 2;x++){
System.out.print(" ");
}
for(int j = 0;j < 2 * n + 1;j++){
System.out.print(ch);
if(j < n){
ch++;
}else{
ch--;
}
}
System.out.print("\n");
}
}
}
输出结果 :