开发者

Java function Else issue

开发者 https://www.devze.com 2022-12-19 17:21 出处:网络
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;

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?


your problem is mismatched braces:

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


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


if (x%2==0){
      return x/2;

change to:

if (x%2==0){
      return x/2;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消