【问题标题】:Having trouble using methods使用方法时遇到问题
【发布时间】: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 类。 isEquilateralTriangle 类的一个方法。错误中的其他方法也是如此。
  • 通过插入关键字 public static boolean isEquilateral (... 将您的方法更改为静态,然后您需要使用 Triangle 类名来调用它们,例如 if (Triangle.isEquilateral(side1...
  • 我该如何称呼这门课?我不确定。
  • 好的,谢谢,它现在似乎可以工作了。
  • 没有必要删除stackoverflow.com/questions/71376497/… - 它可以被修复。

标签: java methods compiler-errors


【解决方案1】:

你必须对一个类有一些引用才能使用它的方法。

Triangle.isRight(side1, side2, side3);

看起来您不需要构造任何三角形,因此每个方法都应该是静态的,如下:

public static boolean isRight(side1, side2, side3){}

查看此链接:accessing another class' methods

如果允许,您也可以删除 homework3_prob3 类并将该 main 方法移动到 Triangle,编译并运行它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 2019-09-28
    • 1970-01-01
    • 2019-10-10
    • 2013-10-29
    • 1970-01-01
    • 2018-12-21
    • 2016-04-28
    相关资源
    最近更新 更多