简体   繁体   中英

Can't find symbol (calling method)

Im new to Java/programming in general I have to write a recursive method to find the greatest common divisor of two numbers. It says I can't find the symbol when I call the method.

   import java.util.Scanner;
public class tester121{
   public static void main(String[]args){
      Scanner input= new Scanner(System.in);
      System.out.println("Enter first number");
      int num1=input.nextInt();
      System.out.println("Enter second number");
      int num2=input.nextInt();
      System.out.println("The Greatest common factor of "+num1+" "+num2+" is "+GCD(num1,num2));


   } 
}

and my method is below. when i try to compile this I get this GCD.java:4: error: class, interface, or enum expected

public class GCD{
public static int GCD(int num1, int num2){
   if(num2==0){
   return num1;
   }
   return(GCD(num2, num1%num2));

}
}

Thanks!

GCD is a method in GCD rather than in the current class so you need

System.out.println("The Greatest common factor of " + num1 + " " + num2
        + " is " + GCD.GCD(num1, num2));

The GCD class itself needs to be in a separate file for it to be declared public

Note by convention method names in Java start with a lowercase letter eg gcd

There are two ways to fix your code and get what you want:

1) Everything in one class (called tester121.java)

 import java.util.Scanner;
public class tester121{
   public static void main(String[]args){
      Scanner input= new Scanner(System.in);
      System.out.println("Enter first number");
      int num1=input.nextInt();
      System.out.println("Enter second number");
      int num2=input.nextInt();
      System.out.println("The Greatest common factor of "+num1+" "+num2+" is "+GCD(num1,num2));
   } 
public static int GCD(int num1, int num2){
   if(num2==0){
   return num1;
   }
   return(GCD(num2, num1%num2));
}
}

put this code in a file called tester121.java which you should then compile.

2) Everything in two separate classes: First class: (save this as GCD.java)

public class GCD{
public static int GCD(int num1, int num2){
   if(num2==0){
   return num1;
   }
   return(GCD(num2, num1%num2));

}
}

Second class (save this as tester121.java)

  import java.util.Scanner;
public class tester121{
   public static void main(String[]args){
      Scanner input= new Scanner(System.in);
      System.out.println("Enter first number");
      int num1=input.nextInt();
      System.out.println("Enter second number");
      int num2=input.nextInt();
      System.out.println("The Greatest common factor of "+num1+" "+num2+" is "+GCD.GCD(num1,num2));


   } 
}

In the second case you need to call GCD.GCD(num1,num2)) because the method is declared to be static

There are multiple errors in your program. First GCD is a class, not a method, so in your main,

System.out.println("The Greatest common factor of "+num1+" "+num2+" is "+GCD(num1,num2));

this is trying to call a "class" and try to feed it with some parameters, which is wrong. Since GCD method is a static method you can do GCD.GCD(..., ...) but most of the time the method name same as the class name should be reserved for constructor method so you would want to find a different name for that method

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