JAVA 打印ABCD菱形

本文介绍了一个使用Java编写的程序,该程序能够打印出特定的字母图案。图案由字母A到E组成,要求打印的行数大于3行且必须为奇数。通过调整输入的行数参数,可以改变图案的大小。


程序如下:

/*
打印出
   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");


}
}
}


输出结果 :


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值