开发者

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?


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜