Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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中的For循环…创建金字塔_Java - Fatal编程技术网

Java中的For循环…创建金字塔

Java中的For循环…创建金字塔,java,Java,我应该编写一个嵌套for循环,打印以下输出: 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 我应该用两种方法 main方法只

我应该编写一个嵌套for循环,打印以下输出:

                   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
我应该用两种方法

main方法只需要从用户那里获得所需的行数。 我应该写另一个叫做printPyramid的方法:

具有行数 打印具有该行数的棱锥体 不返回任何内容。 到目前为止,我已经:

    import java.util.Scanner;

    public class Pyramid {


    //Getting number of rows, calling printPyramid in main method

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    //Get user input = number of lines to print in a pyramid
    System.out.println("Enter the number of lines to produce: ");
    int numLines = input.nextInt();

    //call method:
    printPyramid();

    }//End of main method

    //Making a new method...

    public static void printPyramid (int numLines) {

    int row;
    int col;

    row = 0;
    col = 0;

        for (row = 1; row <= numLines; row++){
            //print out n-row # of spaces
            for (col = 1; col <= numLines-row; col++){
                System.out.print("  ");
            }

            //print out digits = 2*row-1 # of digits printed
            for (int dig= 1; dig <= 2*row-1; dig++){
                System.out.print(row + "  "); 
            }

        System.out.println();
        }//end of main for loop

    }//end of printPyramid

    }//end of class
我有错误,我不知道如何让它正确打印出来。
我相信这些方法都弄糟了

这里有两个大错误。首先,所有Java都是类,所以必须将主方法放在类中。例如:

public class Anything {
    public static void main ...

    public static void printPyramid ...
}
第二,您必须在main中调用printPyramid方法,因为如果不首先调用它,它将不会被执行

public class Anything {
    public static void main ... {
         ...
         printPyramid (numLines);
         ...
    }

    public static void printPyramid ...
}

我希望这些小提示对您有所帮助。

您能发布您的错误吗?如果您对错误一无所知,则很难修复错误。这是家庭作业吗?我有错误。。。您有什么错误?正在添加作业标记…如果不正确,请随意删除它。@dbyrne该标记已被弃用。@dbyrne。非常感谢!我只发现了一个错误。。。java:28:错误:类Pyramid中的printPyramid方法无法应用于给定类型;印刷金字塔;必需:int found:无参数原因:实际参数和形式参数列表在以下方面有所不同length@user1368970这只是一个示例,您必须使用int作为参数调用printPyramid。错误向您解释:必需:int找到:无参数