大家帮忙给出经典的方法

cyuan09 cyuan09 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
今天没事儿做了个题,总感觉方法太笨,但还没想好别的写法,希望大家帮忙给出经典的方法。题目如下:
打印如下图形,
* * * * * * * * *
* * * *
* * * *
* * * *
* * *
* * * *
* * * *
* * * *
* * * * * * * * *
我的方法如下:
public class Test {
public static void main(String[] args) {
Test.printGraphics();
}

public static void printGraphics() {
int total_row = 17;
int total_col = 9;

for(int col = 1; col <= total_col; col++) {
for(int row = total_row; row >= 1; row--) {
if(col != 1 && col != 9) {
if(row == total_row / 2 + 1 || row == total_row / 2 + col || row == col || row == 2 * col - 1) {
System.out.print("* ");
}else {
System.out.print(" ");
}
}else {
if(col == 1 && row <= total_row / 2 + 1) {
System.out.print("* ");
}else if(col == 1 && row > total_row / 2){
System.out.print(" ");
}

if(col == 9 && row > total_row / 2) {
System.out.print("* ");
}else if(col == 9 && row < total_row / 2){
System.out.print(" ");
}
}
}

System.out.println();
}
}
}
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
cyuan09
Java SE 62,621 成员 307,251 内容