简体   繁体   English

使用升序和降序数字在Java中打印居中金字塔

[英]Printing a centered pyramid in Java with ascending and descending numbers

Link to what I want and what I have

I'm trying to print a centered pyramid of 2^n, where 2^row# is the centered number of each row, the numbers to the left are ascending to 2^row# and the numbers to the right are descending. 我正在尝试打印2 ^ n的中心金字塔,其中2 ^ row#是每行的中心编号,左边的数字升至2 ^ row#,右边的数字降序。 I'm pretty new to Java and it took me a really long time to get this much. 我是Java的新手,我花了很长时间才得到这么多。 But now I'm stuck. 但是现在我被卡住了。 The last row is the only row that is correct. 最后一行是唯一正确的行。 I don't know how to make it so 64 is not printed on every line. 我不知道怎么做,所以不是每行都打印64。 Can anyone please give me a hint? 任何人都可以给我一个提示吗?

I've tried messing with every single parameter - starting the last loop with the first row, the last row, changing the starting power, etc. and I just can't figure it out. 我试过弄乱每个参数-从第一行,最后一行开始最后一个循环,更改启动功率等,但我只是想不通。

Thank you for any hints! 谢谢你的任何提示!

public static void main (String [] args){

    int row;
    for (row = 0; row <= 8; row++){ // Prints each row 
        for (int spaces = 8; spaces >= row; spaces --){ // Prints out spaces to left
            System.out.print("  ");
        }

        int power1 = 0; // Power that 2 is being raised to
        for (int i = 0; i < row; i++) { // Prints left side of the pyramid 
            System.out.print(" " + (int)Math.pow(2, power1));
            power1++;
        }

        int power2 = 7;
        for (int i = 1; i < row; i++) { // Prints right side of the pyramid 
            power2--;
            System.out.print(" " + (int)Math.pow(2, power2));       
        }

        System.out.println();
    }       
  }
}

Your problem lies in the fact you always start the right side of the pyramid at 2^7, since you hard code the power2 = 7 decleration and assignment. 你的问题在于你总是以2 ^ 7开始金字塔的右侧,因为你硬编码power2 = 7 decleration和赋值。 If you start this value instead at the current row - 1, you get the behavior you're looking for. 如果您在当前行 - 1处启动此值,则会获得您正在寻找的行为。 Code: 码:

public static void main (String [] args){

int row;
for (row = 0; row <= 8; row++){ // Prints each row 
    for (int spaces = 8; spaces >= row; spaces --){ // Prints out spaces to left
        System.out.print("  ");
    }

    int power1 = 0; // Power that 2 is being raised to
    for (int i = 0; i < row; i++) { // Prints left side of the pyramid 
        System.out.print(" " + (int)Math.pow(2, power1));
        power1++;
    }

    int power2 = row - 1;
    for (int i = 1; i < row; i++) { // Prints right side of the pyramid 
        power2--;
        System.out.print(" " + (int)Math.pow(2, power2));       
    }

    System.out.println();
}

This part is not right. 这部分不对。

        int power2 = 7;
    for (int i = 1; i < row; i++) { // Prints right side of the pyramid 
        power2--;
        System.out.print(" " + (int)Math.pow(2, power2));       
    }

On row 2 you get power2=6 so you display 2^6=64. 在第2行,您得到power2 = 6,因此显示2 ^ 6 = 64。

You should instead be doing something like 你应该做类似的事情

        int power2 = power1;  
    for (int i = 1; i < row; i++) { // Prints right side of the pyramid 
        power2--;
        System.out.print(" " + (int)Math.pow(2, power2));       
    }

You are assigning constant to power2 instead of depending value on row. 您正在为power2分配常量,而不是依赖于行的值。 Can you try this please. 你能试试吗?

int power2 = row-1; int power2 =第1行;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM