首页 > 解决方案 > 我无法让我的金字塔课程打印金字塔

问题描述

我正在尝试打印数字金字塔,但是我无法弄清楚这个错误意味着什么,或者如何解决它。

package com.company;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        pyramid pmid = new pyramid();

        Scanner in = new Scanner(System.in);

        System.out.print("How many rows do you want in your pyramid: 1-10? ");
        int numRows = in.nextInt();
        if (numRows < 1 || numRows > 10){
            System.out.println("ERROR: Number must be greater than zero and less than 11");
            System.exit(0);
        }


    }
}
package com.company;

public class pyramid {
    private int numRow;

    public pyramid(int numRow){
        //this.rows=rows;
        this.numRow=numRow;

        //make three different triangles...
        for(int i=1; i<=numRow; i++){
            for(int j=1; j<=(numRow-i)*2;j++){
                System.out.println(" ");
            }
            for (int k=i;k>=1;k--) {
                System.out.println(" " + k);
            }
            for(int l=2; l<=i;l++){
                System.out.println(" "+l);
                System.out.println(" ");
            }
        }
    }

}

这是我遇到的错误,以及当我尝试修复它时的许多其他错误。

:6:24
java: constructor pyramid in class com.company.pyramid cannot be applied to given types;
  required: int
  found:    no arguments
  reason: actual and formal argument lists differ in length

标签: java

解决方案


简而言之,金字塔中的构造函数将要打印的行数作为参数。然而,在您的原始代码中,您pyramid()无需参数即可调用。numRows尝试使用from Scanner 作为参数调用金字塔方法。

public class Main {
    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.print("How many rows do you want in your pyramid: 1-10? ");
    int numRows = in.nextInt();
    if (numRows < 1 || numRows > 10){
        System.out.println("ERROR: Number must be greater than zero and less than 11");
        System.exit(0);
    } else {
        pyramid pmid = new pyramid(numRows);
    }


}

}


推荐阅读