简体   繁体   中英

Having trouble using methods

I'm relatively new to java and I am having trouble using methods, This is the code:

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");
        }
    }
}

And this is the error produced:

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

I have tried figuring out what's wrong but I am not sure. Is there something specific I have to do to make the command prompt recognize the symbol or can I not put methods in an if statement?

You must have some reference to a class in order to use its methods.

ie

Triangle.isRight(side1, side2, side3);

It also doesn't look like you're in need of constructing any Triangles so each method should be static, following:

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

Check out this link: accessing another class' methods

If you're allowed to, you could also get rid of the homework3_prob3 class and move that main method into Triangle , compiling and running that instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM