没有找到合适的构造函数 [英] No suitable constructor found for
问题描述
我在让 TestPolygon.java 正确运行时遇到问题.The "//创建四个多边形的构造函数,其任何值都不等于默认值或彼此不相等 Polygon[] P_array = {new Polygon(10,5.0,1.0,3.0), new Polygon(6,3,2,5), new Polygon(4,11,4,3), new Polygon(5,12,6,7)}; "我的代码行不断返回错误,因为每个新语句都说没有合适的构造函数找到 Polygon(int,double,double,double).附上图片.
/* 文件:Polygon.java* 作者:Darren Pirtle Jr.* 日期:2016 年 11 月 16 日* 目的:设计一个Java类* 命名多边形*/导入 java.lang.Math;公共类多边形{私人整数 numSides;私人双面长度;私人双 xCoord;私人双yCoord;私人双颂;私人双周界;//使用默认值创建多边形的构造函数公共多边形(){数量边 = 4;边长 = 10.0;x坐标 = 0.0;y坐标 = 0.0;apothem = 5.0;}//使用指定数字的构造函数公共多边形(int s,双sL,双x,双y,双a,双p){setNumSides(s);设置边长(sL);setXCoord(x);setYCoord(y);setApothem(a);}//一个 getArea() 方法,它返回多边形面积的双精度值公共双 getArea() {返回 .5*心律*周长;}//所有数据字段的 getter 和 setter公共 int setNumSides(int s){numSides = s;返回 numSides;}公共双setSideLength(双sL){边长 = sL;返回边长;}公共双 setXCoord(double x){x坐标 = x;返回 xCoord;}公共双集YCoord(双Y){y坐标 = y;返回y坐标;}public double setApothem(double a){apothem = a;回首诗;}公共双setPerimeter(双p){周长 = p;返回周长;}公共 int getNumSides() {返回 numSides;}公共双 getSideLength() {返回边长;}公共双 getXCoord() {返回 xCoord;}公共双 getYCoord() {返回y坐标;}公共双 getApothem() {回首诗;}公共双 getPerimeter() {返回周长;}//以字符串格式显示值的toString()公共字符串 toString(){return "("+numSides+", "+String.format("%.1f",sideLength)+", "+String.format("%.1f",xCoord)+", "+String.format("%.1f",yCoord)+")";}}
<小时>
/* 文件:Polygon.java* 作者:Darren Pirtle Jr.* 日期:2016 年 11 月 16 日* 目的:设计一个Java类* 命名多边形*///导入必要的包导入 java.util.Scanner;导入 java.text.DecimalFormat;导入 java.lang.Math;类测试多边形{公共静态无效主(字符串 [] args){//构造一个不带参数的多边形多边形 P = 新多边形();//创建四个多边形的构造函数,其任何值均不等于默认值或彼此不相等Polygon[] P_array = {new Polygon(10,5.0,1.0,3.0), new Polygon(6,3,2,5), new Polygon(4,11,4,3), new Polygon(5,12,6),7)};//调用所有方法并显示结果的构造函数System.out.println("toString() 结果:"+P);System.out.println("getNumSides() 结果:"+ P.getNumSides());System.out.println("getSideLength() 结果:"+ P.getSideLength());System.out.println("getXCoord() 结果:"+ P.getXCoord());System.out.println("getYCoord() 结果:"+ P.getYCoord());System.out.println("getPerimeter() 结果:"+ P.getPerimeter());System.out.println("setNumSides(4) 结果:"+ P.setNumSides(4));System.out.println("setSideLength(3.0) 结果:"+ P.setSideLength(3.0));System.out.println("setXCoord(2.0) 结果:"+ P.setXCoord(2));System.out.println("setYCoord(2.0) 结果:"+ P.setYCoord(2));for(int i=0; i<P_array.length; i++){System.out.println("toString() 结果:"+P_array[i]);System.out.println("getNumSides() 结果:"+ P_array[i].getNumSides());System.out.println("getSideLength() 结果:"+ P_array[i].getSideLength());System.out.println("getXCoord() 结果:"+ P_array[i].getXCoord());System.out.println("getYCoord() 结果:"+ P_array[i].getYCoord());System.out.println("getPerimeter() 结果:"+ P_array[i].getPerimeter());System.out.println("setNumSides(4) 结果:"+ P_array[i].setNumSides(4));System.out.println("setSideLength(3.0) 结果:"+ P_array[i].setSideLength(3.0));System.out.println("setXCoord(2.0) 结果:"+ P_array[i].setXCoord(2));System.out.println("setYCoord(2.0) 结果:"+ P_array[i].setYCoord(2));}}}
错误信息说明了一切:
<块引用>没有找到适合 Polygon(int,double,double,double) 的构造函数.
这只是意味着 Java 无法找到带有参数 (int, double, double, double)
的类 Polygon 的构造函数(这是您在创建 Polygon 对象时使用的)>
如果您查看 Polygon 类的构造函数:
您创建了 2 个构造函数:
无参数构造函数
带有 6 个参数的构造函数(int、double、double、double、double、double)
因此,在创建 Polygon 对象时,请确保您正在执行:
new Polygon();或者新多边形(整数,双,双,双,双,双);
示例:
Polygon p1 = new Polygon();多边形 p2 = 新多边形(10, 2.5, 3.5, 4.5, 5.5, 6.5);多边形 p3 = 新多边形(10, 20, 30, 40, 50, 60);//这个也可以
现在,您只提供了 4 个参数:
new Polygon(10, 5.0, 1.0, 3.0),^ ^ ^ ^||||int d d d(d 表示双精度)
I am having issues with getting the TestPolygon.java to run correctly. THe "//Constructor that creates four polygons with any value that are not equal to the defualt value or to each other Polygon[] P_array = {new Polygon(10,5.0,1.0,3.0), new Polygon(6,3,2,5), new Polygon(4,11,4,3), new Polygon(5,12,6,7)}; " line of my code keeps coming back with an error for every new statement saying that "no suitable constructor found for Polygon(int,double,double,double). Picture is attached.
/* File: Polygon.java
* Author: Darren Pirtle Jr.
* Date: November 16, 2016
* Purpose: Design a Java class
* named Polygon
*/
import java.lang.Math;
public class Polygon {
private int numSides;
private double sideLength;
private double xCoord;
private double yCoord;
private double apothem;
private double perimeter;
//Constructor that creates a Polygon using the defaults
public Polygon(){
numSides = 4;
sideLength = 10.0;
xCoord = 0.0;
yCoord = 0.0;
apothem = 5.0;
}
//Constructor that uses the specified numbers
public Polygon(int s,double sL,double x,double y,double a, double p){
setNumSides(s);
setSideLength(sL);
setXCoord(x);
setYCoord(y);
setApothem(a);
}
// A getArea() method that returns a double value for the area of the polygon
public double getArea() {
return .5*apothem*perimeter;
}
// Getter and setter for all data fields
public int setNumSides(int s){
numSides = s;
return numSides;
}
public double setSideLength(double sL){
sideLength = sL;
return sideLength;
}
public double setXCoord(double x){
xCoord = x;
return xCoord;
}
public double setYCoord(double y){
yCoord = y;
return yCoord;
}
public double setApothem(double a){
apothem = a;
return apothem;
}
public double setPerimeter(double p){
perimeter = p;
return perimeter;
}
public int getNumSides() {
return numSides;
}
public double getSideLength() {
return sideLength;
}
public double getXCoord() {
return xCoord;
}
public double getYCoord() {
return yCoord;
}
public double getApothem() {
return apothem;
}
public double getPerimeter() {
return perimeter;
}
//A toString() that displays the values in string format
public String toString(){
return "("+numSides+", "+String.format("%.1f",sideLength)+", "+String.format("%.1f",xCoord)+", "+String.format("%.1f",yCoord)+")";
}
}
/* File: Polygon.java
* Author: Darren Pirtle Jr.
* Date: November 16, 2016
* Purpose: Design a Java class
* named Polygon
*/
//Import necessary packages
import java.util.Scanner;
import java.text.DecimalFormat;
import java.lang.Math;
class TestPolygon{
public static void main(String[] args){
//Constructor that creates one polygon with no argument
Polygon P = new Polygon();
//Constructor that creates four polygons with any value that are not equal to the defualt value or to each other
Polygon[] P_array = {new Polygon(10,5.0,1.0,3.0), new Polygon(6,3,2,5), new Polygon(4,11,4,3), new Polygon(5,12,6,7)};
//Constructor that calls all methods and displays results
System.out.println("toString() results: "+P);
System.out.println("getNumSides() results: "+ P.getNumSides());
System.out.println("getSideLength() results: "+ P.getSideLength());
System.out.println("getXCoord() results: "+ P.getXCoord());
System.out.println("getYCoord() results: "+ P.getYCoord());
System.out.println("getPerimeter() results: "+ P.getPerimeter());
System.out.println("setNumSides(4) results: "+ P.setNumSides(4));
System.out.println("setSideLength(3.0) results: "+ P.setSideLength(3.0));
System.out.println("setXCoord(2.0) results: "+ P.setXCoord(2));
System.out.println("setYCoord(2.0) results: "+ P.setYCoord(2));
for(int i=0; i<P_array.length; i++)
{
System.out.println("toString() results: "+P_array[i]);
System.out.println("getNumSides() results: "+ P_array[i].getNumSides());
System.out.println("getSideLength() results: "+ P_array[i].getSideLength());
System.out.println("getXCoord() results: "+ P_array[i].getXCoord());
System.out.println("getYCoord() results: "+ P_array[i].getYCoord());
System.out.println("getPerimeter() results: "+ P_array[i].getPerimeter());
System.out.println("setNumSides(4) results: "+ P_array[i].setNumSides(4));
System.out.println("setSideLength(3.0) results: "+ P_array[i].setSideLength(3.0));
System.out.println("setXCoord(2.0) results: "+ P_array[i].setXCoord(2));
System.out.println("setYCoord(2.0) results: "+ P_array[i].setYCoord(2));
}
}
}
The error message says it all:
no suitable constructor found for Polygon(int,double,double,double).
It simply means that Java is unable to find a constructor for class Polygon with argument (int, double, double, double)
(which is what you have used when creating a Polygon object)
If you look at your constructors for class Polygon:
You created 2 constructors:
No arguments constructor
Constructor with 6 arguments (int, double, double, double, double, double)
Hence, when creating a Polygon object, ensure you are doing:
new Polygon();
or
new Polygon(int, double, double, double, double, double);
Example:
Polygon p1 = new Polygon();
Polygon p2 = new Polygon(10, 2.5, 3.5, 4.5, 5.5, 6.5);
Polygon p3 = new Polygon(10, 20, 30, 40, 50, 60); //this is ok too
Right now, you are providing only 4 arguments:
new Polygon(10, 5.0, 1.0, 3.0),
^ ^ ^ ^
| | | |
int d d d (d for double)
这篇关于没有找到合适的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!