Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的数字金字塔。_Java - Fatal编程技术网

java中的数字金字塔。

java中的数字金字塔。,java,Java,我需要用java打印出一个数字金字塔。它是2的倍数,还有下面的空格。但是我的代码只显示了1的倍数,没有空格 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

我需要用java打印出一个数字金字塔。它是2的倍数,还有下面的空格。但是我的代码只显示了1的倍数,没有空格

                        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
这是我的密码

import java.util.Scanner;

public class NumericPyramid {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        //Taking noOfRows value from the user

        System.out.println("How Many Rows You Want In Your Pyramid?");

        int noOfRows = sc.nextInt();

        //Initializing rowCount with 1

        int rowCount = 1;

        System.out.println("Here Is Your Pyramid");

        //Implementing the logic

        for (int i = noOfRows; i > 0; i--) {
            //Printing i*2 spaces at the beginning of each row

            for (int j = 1; j <= i*2; j++) {
                System.out.print(" ");
            }

            //Printing j value where j value will be from 1 to rowCount

            for (int j = 1; j <= rowCount; j++) {
                System.out.print(j+" ");
            }

            //Printing j value where j value will be from rowCount-1 to 1

            for (int j = rowCount-1; j >= 1; j--) {                 
                System.out.print(j+" ");             
            }                          

            System.out.println();

            //Incrementing the rowCount

            rowCount++;
        }
    }
}

对于从1到n的每个j,需要写2^j。目前你只写j。 所以写一个函数,对于给定的k返回2^k

编辑:对于较大的n,需要使用biginger:

import java.math.BigInteger;

public class NumericPyramid {
    private static BigInteger pow(int exponent) {
        BigInteger result = new BigInteger("1");
        BigInteger two = new BigInteger("2");
        for (int i = 0; i < exponent; i++) {
            result = result.multiply(two);
        }
        return result;
    }


金字塔是1*2=2,2*2=4,4*2=8,依此类推,然后倒过来,8/2=4,4/2=2,2/2=1,当中心

for (int i = 0; i < noOfRows; i++) {
            int cont = 1;
            for (int j = 0; j <= i; j++) {
                System.out.print(cont + " ");
                cont = cont * 2;
            }
            cont = cont / 2;
            for (int j = 0; j < i; j++) {
                cont = cont / 2;
                System.out.print(cont + " ");
            }
            System.out.println();
        }
for(int i=0;i对于(int j=0;j)如果每次你把
j
增加一倍,你会怎么办?每次你把
j
减少一倍,你会把它减半吗?你是什么意思?@DavidWallace我知道你在那里做了什么(可悲的是),OP做了(可悲的)不,你可能想问一个问题。
1
i*2/2
的意义是什么?它与
i
相同。乘以2,然后除以2,就剩下原来的数字(除非溢出)。你需要添加空格,这样它就成了金字塔的形式
System.out.print(j+" ");  
System.out.print(pow(j)+" "); 
for (int i = 0; i < noOfRows; i++) {
            int cont = 1;
            for (int j = 0; j <= i; j++) {
                System.out.print(cont + " ");
                cont = cont * 2;
            }
            cont = cont / 2;
            for (int j = 0; j < i; j++) {
                cont = cont / 2;
                System.out.print(cont + " ");
            }
            System.out.println();
        }