我需要帮助使用嵌套for循环创建此形状[更新]

时间:2014-10-24 20:01:11

标签: java nested-loops

我需要使用嵌套for循环打印出这个形状作业(完全披露。) 我不知道如何将整个事情集中在一起。

enter image description here

这些时期代表了模式的延续。所以它应该是整个金字塔。

这就是我所拥有的现在

    public static void question4(){

   int ix = 30;
for(int i = 1; i<=128; i=i*2){

// x is the number printed
//it gets the value from i,
  for (int g = ix; g>=0; g--){
      System.out.print(" ");
  }
  for (int x2 =1; x2<=i-1; x2=x2*2){

      System.out.print("   ");
      System.out.print(x2);

  }
  for (int x = i; x>=1; x=x/2){
      System.out.print("   ");
      System.out.print(x);

  }

 ix=ix-4;
  System.out.println();

}

感谢有关减量空间的帮助,现在数字本身会推动底部行。我尝试使用另一个用户建议的string.length命令,但它一直返回错误。

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:2)

尝试这样的事情:

public static void main(String[] args) {
        String spacer = "                         ";
        for (int i = 1; i <= 128; i = i * 2) {

            // x is the number printed
            // it gets the value from i,
            System.out.print(spacer);
            for (int x2 = 1; x2 <= i - 1; x2 = x2 * 2) {
                System.out.print(" ");
                System.out.print(x2);
            }
            for (int x = i; x >= 1; x = x / 2) {
                System.out.print(" ");
                System.out.print(x);
            }
            if ((i * 2) < 10)
                spacer = spacer.substring(0, spacer.length() - 2);
            else if ((i * 2) < 100)
                spacer = spacer.substring(0, spacer.length() - 3);
            else
                spacer = spacer.substring(0, spacer.length() - 4);
            System.out.println();
        }
}

当你沿着三角形向下移动时,这会为每一行创造空间,从而减少空间。

给出输出:

                      1
                    1 2 1
                  1 2 4 2 1
                1 2 4 8 4 2 1
             1 2 4 8 16 8 4 2 1
          1 2 4 8 16 32 16 8 4 2 1
       1 2 4 8 16 32 64 32 16 8 4 2 1
   1 2 4 8 16 32 64 128 64 32 16 8 4 2 1

答案 1 :(得分:1)

我知道回答它很容易。首先检查此代码

    int spaces = 7;
    for(int i = 1; i<=128; i=i*2){

         for(int k=1;k<=spaces;k++)
              System.out.print("  ");
         spaces--;

    // x is the number printed
    //it gets the value from i,
      for (int x2 =1; x2<=i-1; x2=x2*2){

          System.out.print(" ");
          System.out.print(x2);

      }
      for (int x = i; x>=1; x=x/2){
          System.out.print(" ");
          System.out.print(x);

      }
      System.out.println();
}

正如您所看到的,每当第一个循环开始迭代时,嵌套for循环以获取空间,在另一个嵌套循环打印数字之前给出空间。随着循环移动,空间将减少,金字塔将最终增长。

答案 2 :(得分:0)

虽然回答&amp;接受,这是一个解决方案,打印一个完美的金字塔与一个for循环

public static void perfectPyramid() {
    int upLimit = 1024;
    int blankFieldWidth = String.valueOf(upLimit).length() + 1; // if upLimit is 3-digit, the blank field will be 4-blanks
    String blank = new String(new char[blankFieldWidth]).replace("\0", " "); //one-liner for creating a String by repeating another String a given number of times
    String numPart = "1" + new String(new char[blankFieldWidth - String.valueOf(blankFieldWidth - 1).length()]).replace("\0", " ");
    String previous = "-"; // dummy initial value
    for (int i = 1; i <= upLimit; i = i * 2) {
        int countOfBlankFields = (int) (Math.log(upLimit / i) / Math.log(2)); // the count of blank columns per row (one side only)
        String dynSpacer = new String(new char[blankFieldWidth - String.valueOf(i).length()]).replace("\0", " ");
        numPart = numPart.replace(previous,  previous +  i + dynSpacer + previous);
        String blanks = new String(new char[countOfBlankFields]).replace("\0", blank);
        String row = blanks + numPart + blanks;
        previous = i + dynSpacer;
        System.out.println(row);
    }
}

它考虑到空格

打印金字塔
                                                  1                                                      
                                             1    2    1                                                 
                                        1    2    4    2    1                                            
                                   1    2    4    8    4    2    1                                       
                              1    2    4    8    16   8    4    2    1                                  
                         1    2    4    8    16   32   16   8    4    2    1                             
                    1    2    4    8    16   32   64   32   16   8    4    2    1                        
               1    2    4    8    16   32   64   128  64   32   16   8    4    2    1                   
          1    2    4    8    16   32   64   128  256  128  64   32   16   8    4    2    1              
     1    2    4    8    16   32   64   128  256  512  256  128  64   32   16   8    4    2    1         
1    2    4    8    16   32   64   128  256  512  1024 512  256  128  64   32   16   8    4    2    1    

对于嵌套循环的问题,您可以通过将字符串替换为使用嵌套的

来创建行。