【发布时间】:2021-10-20 02:25:39
【问题描述】:
我对java比较陌生,我在使用方法时遇到了麻烦,这是代码:
import java.util.Scanner;
class Triangle {
public boolean isRight (double a, double b, double c) {
if (a*a + b*b == c*c) {
return true;
}
else {
return false;
}
}
public boolean isEquilateral (double a, double b, double c) {
if (a == b && a == c) {
return true;
}
else {
return false;
}
}
public boolean isScalene (double a, double b, double c) {
if (a != b && a != c && b != c) {
return true;
}
else {
return false;
}
}
public boolean isIsosceles (double a, double b, double c) {
if ((a != b && a == c) || (a != c && a == b) || (b == c && b != c)) {
if (a != c && b != c) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
}
class homework3_prob3 {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println ("Enter the length of the first side: ");
double side1 = scan.nextDouble();
System.out.println ("Enter the length of the second side: ");
double side2 = scan.nextDouble();
System.out.println ("Enter the length of the third side: ");
double side3 = scan.nextDouble();
if (isEquilateral(side1, side2, side3) == true){
System.out.println ("The triangle is equilateral");
}
else if (isScalene(side1, side2, side3) == true){
System.out.println ("The triangle is scalene");
}
else if (isIsosceles(side1, side2, side3) == true){
System.out.println ("The triangle is isosceles");
}
if (isRight(side1, side2, side3) == true){
System.out.println ("The triangle is also right");
}
}
}
这是产生的错误:
homework3_prob3.java:58: error: cannot find symbol
if (isEquilateral(side1, side2, side3) == true){
^
symbol: method isEquilateral(double,double,double)
location: class homework3_prob3
homework3_prob3.java:61: error: cannot find symbol
else if (isScalene(side1, side2, side3) == true){
^
symbol: method isScalene(double,double,double)
location: class homework3_prob3
homework3_prob3.java:64: error: cannot find symbol
else if (isIsosceles(side1, side2, side3) == true){
^
symbol: method isIsosceles(double,double,double)
location: class homework3_prob3
homework3_prob3.java:67: error: cannot find symbol
if (isRight(side1, side2, side3) == true){
^
symbol: method isRight(double,double,double)
location: class homework3_prob3
4 errors
我已经尝试找出问题所在,但我不确定。是否需要做一些特定的事情才能使命令提示符识别符号,或者我不能将方法放在 if 语句中?
【问题讨论】:
-
您根本没有使用您在
main()中创建的Triangle类。isEquilateral是Triangle类的一个方法。错误中的其他方法也是如此。 -
通过插入关键字
public static boolean isEquilateral (...将您的方法更改为静态,然后您需要使用Triangle类名来调用它们,例如if (Triangle.isEquilateral(side1... -
我该如何称呼这门课?我不确定。
-
好的,谢谢,它现在似乎可以工作了。
-
没有必要删除stackoverflow.com/questions/71376497/… - 它可以被修复。
标签: java methods compiler-errors