for loop双楔形

时间:2015-03-26 02:59:29

标签: java for-loop

所以对于一项任务,我需要制作一个带有for循环的双楔形图,到目前为止我没有运气,有人可以帮忙吗?

以下是结果的示例:

*******
*** ***
**   **
*     *

这是我的代码

  int dual_wedge_length=9;
  int half_length = dual_wedge_length/2;
  int space=1;
  int height2 = (dual_wedge_length/2) +1;

  for (int line1 = 1; line1 <= dual_wedge_length; line1++)
  {
     System.out.print("*");
  }
  System.out.println();

  for (int height = 1; height <= (dual_wedge_length+1)/2; height++)
  {   
     for (int half1 = 1; half1 <= half_length; half1++)
     {
        System.out.print("*");
        //half_length--;
        space+=2;
     }

     for (int space_counter = 0; space_counter == space;space_counter++)
     {
        System.out.print(".");
     }

     for (int half1 = 1; half1 >= half_length; half1++)
     {
        System.out.print("*");
        half_length--;
     }
     System.out.println();`

1 个答案:

答案 0 :(得分:0)

    int dual_wedge_length=9;
    int height2 = (dual_wedge_length+1)/2;
    for(int i = 0;i < dual_wedge_length; i++)System.out.print("*");
    System.out.println();
    for(int i = 1; i < height2;i++){
        int num = height2 - i;
        for(int j = 0; j < num; j++){
            System.out.print("*");
        }
        for(int k = 0; k < 2*i -1; k++){
            System.out.print(" ");
        }
        for(int m = 0; m < num; m++){
            System.out.print("*");
        }
        System.out.println();

    }