Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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
具有2的倍数的java金字塔不工作_Java_For Loop_Pyramid - Fatal编程技术网

具有2的倍数的java金字塔不工作

具有2的倍数的java金字塔不工作,java,for-loop,pyramid,Java,For Loop,Pyramid,我假设创建一个金字塔,将每个数字乘以2,直到到达中间,然后除以2,如下面的示例所示 但是,在编写代码后,我无法使数字加倍(I*2),然后一旦它到达中心,它就会除以2,直到它变成1 我的输出: package question5; public class Pyramid { public static void main(String[] args) { int x = 5; int rowCount = 1; System.out.println("Her

我假设创建一个金字塔,将每个数字乘以2,直到到达中间,然后除以2,如下面的示例所示

但是,在编写代码后,我无法使数字加倍(I*2),然后一旦它到达中心,它就会除以2,直到它变成
1

我的输出:

 package question5;

 public class Pyramid {

 public static void main(String[] args) {
    int x = 5;
    int rowCount = 1;

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

    //Implementing the logic

    for (int i = x; 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++;
    }
}
}

package问题5;
公共阶层金字塔{
公共静态void main(字符串[]args){
int x=5;
int rowCount=1;
System.out.println(“这是你的金字塔”);
//实现逻辑
对于(int i=x;i>0;i--)
{
//在每行开头打印i*2个空格

对于(int j=1;j现在您正在输出
j
,一个从
1
5
的数字。您想要输出2j-1,这很简单:
1这很有效……您可以使用math.pow方法

public class test {

     public static void main(String[] args) {
        int x = 5;
        int rowCount = 1;

        System.out.println("Here Is Your Pyramid");
        //Implementing the logic

        for (int i = x; 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 = 0; j <= rowCount-1; j++)             
            {
            System.out.printf("%2d", (int)Math.pow(2, j));  
            }

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

            for (int j = rowCount-1; j >= 1; j--)
            {                    
            System.out.printf("%2d", (int)Math.pow(2, j-1));
            }                          

            System.out.println();

            //Incrementing the rowCount

            rowCount++;

        }
    }
    }
公共类测试{
公共静态void main(字符串[]args){
int x=5;
int rowCount=1;
System.out.println(“这是你的金字塔”);
//实现逻辑
对于(int i=x;i>0;i--)
{
//在每行开头打印i*2个空格

对于(int j=1;j您希望我为loopNo输入吗?只需替换两个
System.out.print(j+“”)
好的,谢谢,我现在就这么做,我在代码中放了一张图片,它看起来很奇怪?我在代码中放了一张图片,它看起来很奇怪?这好像你有一个整数序列,你需要把它们映射到一个几何序列上。一旦你找到了公式,你的程序就正确了。
public class test {

     public static void main(String[] args) {
        int x = 5;
        int rowCount = 1;

        System.out.println("Here Is Your Pyramid");
        //Implementing the logic

        for (int i = x; 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 = 0; j <= rowCount-1; j++)             
            {
            System.out.printf("%2d", (int)Math.pow(2, j));  
            }

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

            for (int j = rowCount-1; j >= 1; j--)
            {                    
            System.out.printf("%2d", (int)Math.pow(2, j-1));
            }                          

            System.out.println();

            //Incrementing the rowCount

            rowCount++;

        }
    }
    }