大家帮忙给出经典的方法
今天没事儿做了个题,总感觉方法太笨,但还没想好别的写法,希望大家帮忙给出经典的方法。题目如下:
打印如下图形,
* * * * * * * * *
* * * *
* * * *
* * * *
* * *
* * * *
* * * *
* * * *
* * * * * * * * *
我的方法如下:
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();
}
}
}