我目前正在制作一个程序,告诉您用户输入的三角形类型,周长和面积。我目前收到的错误"实际和正式的参数列表长度不同",我尝试过谷歌搜索解决方案,但我不太确定导致该错误的原因。
import java.util.*;
public class TriangleTesterID8876 {
public static void main(String[] args) {
Triangle userTriangle = new Triangle();
Scanner kb = new Scanner(System.in);
System.out.println("==================================");
System.out.println(" This program is designed for");
System.out.println(" the user to input a triangle");
System.out.println(" and calculate the area, ");
System.out.println(" perimeter, and type of triangle");
System.out.println("==================================");
//Side 1
System.out.println("Please input the first side.");
double side1 = kb.nextDouble();
userTriangle.setSide1(side1);
//Side 2
System.out.println("Please input the second side.");
double side2 = kb.nextDouble();
userTriangle.setSide2(side2);
//Side 3
System.out.println("Please input the third side.");
double side3 = kb.nextDouble();
userTriangle.setSide3(side3);
System.out.println(userTriangle.typeString());
}
}
Triangle.java:
public class Triangle {
private double mySide1;
private double mySide2;
private double mySide3;
public String backHalf;
public void setSide1(double side1) {
mySide1 = side1;
}
public void setSide2(double side2) {
mySide2 = side2;
}
public void setSide3(double side3) {
mySide3 = side3;
}
public double getSide1() {
return mySide1;
}
public double getSide2() {
return mySide2;
}
public double getSide3() {
return mySide3;
}
private boolean isValid() {
if ((mySide1 < mySide2 + mySide3) && (mySide2 < mySide1 + mySide3) && (mySide3 < mySide1 + mySide2)) {
return true;
} else
return false;
}
public boolean isEquilateral() {
if ((mySide1 == mySide2) && (mySide2 == mySide3)) {
return true;
} else {
return false;
}
}
public boolean isIsoscelese() {
if (mySide1 == mySide2 || mySide1 == mySide3 || mySide1 == mySide2) {
return true;
} else {
return false;
}
}
public boolean isScalene() {
if (mySide1 != mySide2 && mySide2 != mySide1 && mySide2 != mySide3) {
return true;
} else {
return false;
}
}
public double perimeter() {
return mySide1 + mySide2 + mySide3;
}
public double findArea() {
double s = perimeter() / 2;
return Math.sqrt(s * ((s - mySide1) * (s - mySide2) * (s - mySide3)));
}
public String toTypeString() {
double perimeter = perimeter();
double area = findArea();
backHalf = mySide1 + " " + mySide2 + " " + mySide3 + ", a perimeter of " + perimeter + ", and an area of " + area + ".";
return backHalf;
}
public String typeString(String output) {
if (isValid() == true) {
if (isEquilateral()) {
output = "This is an equilateral triangle with sides " + backHalf;
return output;
} else if (isIsoscelese()) {
output = "This is an isosceles triangle with sides " + backHalf;
return output;
} else if (isScalene()) {
output = "This is a scalene triangle with sides " + backHalf;
return output;
}
} else
output = "Please input a valid triangle.";
return output;
}
}
答案 0 :(得分:4)
您使用单个String参数声明了typeString()
,但是您正在调用它 - userTriangle.typeString()
- 没有任何参数。
看起来您应该删除该参数:
改变:
public String typeString(String output) {
到
public String typeString() {
String output = null;
....
}
答案 1 :(得分:0)
typeString()
接受一个字符串作为参数。在你的情况下,你称之为空
答案 2 :(得分:0)
你的Triangle
课说typeString(String output)
应该有一个参数,但你没有参数这样使用。
System.out.println(userTriangle.typeString());
使用
System.out.println(userTriangle.typeString());
as
System.out.println(userTriangle.typeString(output));
OR
将您的Triangle
班级方法签名typeString(String output)
更改为typeString()
。