java - 我无法让我的金字塔课程打印金字塔
问题描述
我正在尝试打印数字金字塔,但是我无法弄清楚这个错误意味着什么,或者如何解决它。
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()
无需参数即可调用。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);
}
}
}
推荐阅读
- html - CSS:倾斜和旋转问题
- asp.net-core - 在 Web API Core 3.1 中实现刷新令牌
- javascript - React 不会在初始加载时将数据加载到组件中
- node.js - 使用strapi和mongo创建项目时出错
- kubernetes - 入口控制器多个主机
- node.js - MongoDB对象属性检查嵌套数组中是否存在具有特定值的$
- python - 如果python中的特定字符串不同,是否有复制一行的方法?
- swift - 如何以编程方式创建重叠栏按钮项
- react-native - 无法使用 React-Native-Tts 给出错误 Native 模块无法加载
- database - PDF 文件到 wp-content 上传文件夹