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

解决方案 »

  1.   

    帮你顶一下
    int total_row = 17;
    int total_col = 9;
    最好用参数传进来
      

  2.   

        public static void main(String[] args) {
            int[] s = {9,4,4,4,3,4,4,4,9};
            for(int i=0;i<9;i++){
             int n = s[i];
         if(i > 0&& i < 8)
             System.out.print("\n ");
         else if (i == 0)
         System.out.print(" ");
         else System.out.print("\n");
             for(int k=0;k<n;k++){
                 System.out.print("*");
             }
            }
        }这个确实很无聊 
      

  3.   

    再来个灵活点的
       public static void main(String[] args) {
            int t = 1;
            int[] s = {9*t,4*t,4*t,4*t,3*t,4*t,4*t,4*t,9*t};
            for(int i=0;i < s.length;i++){
             int n = s[i];
         if(i > 0&& i < s.length - 1)
             System.out.print("\n ");
         else if (i == 0)
         System.out.print(" ");
         else System.out.print("\n");
             for(int k=0;k<n;k++){
                 System.out.print("*");
             }
            }
        }
      

  4.   

    我认为,解决方法越简单越好。
    所以,用下面代码。
    System.out.println("  * * * * * * * * *");
    System.out.println("* * * * *");
    System.out.println("* * * * *");
    System.out.println("  * * * *");
    System.out.println("  * * *");
    System.out.println("  * * * *");
    System.out.println("  * * * *");
    System.out.println("  * * * *");
    System.out.println("* * * * * * * * *");