Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java function Else issue

Sooo I'm having an issue with my function.

static int syracuse(int x){
    if (x%2==0){
      return x/2;
    else{
      return 3*x+1;
    }
   }
  }

Well soo my issue is : if x is even, return x/2 OR is x if odd. returns 3x+1. But when I try to compile java tells me that ( 'else' with 'if') I don't know what to do :\

why would I need a else if?

like image 877
Sally Walker Avatar asked Jun 09 '26 18:06

Sally Walker


2 Answers

your problem is mismatched braces:

static int syracuse(int x){
    if (x%2==0){
      return x/2;
    } else {
      return 3*x+1;
    }
}
like image 200
ninesided Avatar answered Jun 11 '26 10:06

ninesided


Your braces are wrongly placed.

static int syracuse(int x){
    if (x%2==0){
      return x/2;
    }
    else{
      return 3*x+1;
    }
}

PS: I'm not an java expert, so I'm not sure x/2 can be cast as int on return

like image 39
o.k.w Avatar answered Jun 11 '26 09:06

o.k.w