簡體   English   中英

我無法讓我的金字塔 class 打印金字塔

[英]I cannot get my pyramid class to print the pyramid

我正在嘗試打印數字金字塔,但是我無法弄清楚這個錯誤意味着什么,或者如何解決它。

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

簡而言之,金字塔中的構造函數將要打印的行數作為參數。 然而,在您的原始代碼中,您調用pyramid()沒有參數。 嘗試使用 Scanner 中的numRows作為參數調用金字塔方法。

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);
    }


}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM