-1

It's a code to get a common divisor. The first code fails and the second code does not(This method must return a result of type int). I really want to know why....

If we don't have a common divisor, we want to return -1.

public class aaa{
   public static int function(int a, int b){
         for(int a = 1; i <= a; i++){
             if ( a % i == 0){ 
                 b--;
             }if ( b == 0) {
                 return i;
             }else {
                 return -1;
             }
         }
   }
   public static void main(String[] args) {
          system.out.println("The 5th common measure of 1000 is" + fuction(1000, 5));
   }
}
public class aaa{
   public static int function(int a, int b){
         for(int a = 1; i <= a; i++){
             if ( a % i == 0){ 
                 b--;
             }if ( b == 0) {
                 return i;
             }
         } return -1;
   }
   public static void main(String[] args) {
          system.out.println("The 5th common measure of 1000 is" + fuction(1000, 5));
   }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    What will your function return if `a` is less than `1`? No `return` will ever be hit. – Ivar Jun 07 '22 at 09:52
  • In the `b--` case int he first snippet there is no `return`, the compiler cannot whether or not a method invocation will always hit one of the other two cases eventually. – luk2302 Jun 07 '22 at 09:52
  • 1
    Try to ask yourself "Do all possible paths return some value?" – Ricky Mo Jun 07 '22 at 09:56
  • Neither of these programs makes sense, though, because your loop variable is `a` rather than `i`, and `i` is never defined. – RealSkeptic Jun 07 '22 at 09:58

1 Answers1

1

Your code has 2 separate bugs in it which makes your question moot.

Why you get this error

As I mentioned, your method doesn't in any way calculate divisors so perhaps you shouldn't be focused on this in the first place. But, hey, you asked.

(This method must return a result of type int). I really want to know why....

You look at your code, your brain makes a few assumptions (for example, a and b are both obviously more than 1, who would ask for the greatest common divisor of 1, right? Or even worse, 0?) - and then goes: Wha? This method obviously always returns, clearly. What the heck is the compiler on about?

But that's not how a computer works. That info about 'a cannot, clearly, ever be below 2' isn't written anywhere, so the compiler does not assume it.

If a is 0, then no return statement is ever executed, so what should this method do then? That is what the compiler is complaining about.

Your code doesn't do what you think it does

Your code has a bug in it, clearly you meant to write for (int i = 1, not for (int a = 1) - that is a separate issue. Code can have more than a single problem, of course.

A second more important issue is that your code doesn't work at all in the first place: Your loop never loops - it enters once (even if you fix the above problem), with i being 1, and then:

  • if a % 1 is 0, which it will always be, decrement b (why? This is wrong)
  • Then just return false - stop the entire method.

You never even loop.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I think the point of the exercise is to find f(a,b) which is the b'th measure of a, not the common divisor of a and b. In that light, decrementing b makes sense, and I think the second version of the program may actually work. – RealSkeptic Jun 07 '22 at 10:11