简体   繁体   English

无法使用布尔值在if语句内返回

[英]Can't return inside an if statement, using boolean

I'm trying to make a boolean method, but it's not recognizing I have a return statement. 我正在尝试制作一个布尔方法,但是它没有意识到我有一个return语句。 What should I do? 我该怎么办?

public boolean isThreeKind( int rankHist[]) {
  for (int i=0;i<=13;i++){
      if (rankHist[i]>=3){
          return true;
      }else{
          return false;
      }

  }
}

Your code does not make sense. 您的代码没有意义。 There is no point in having a loop if you're always going to run the code inside the loop exactly once. 如果您总是要在循环内仅运行一次代码,则没有循环的意义。 I think you must have misunderstood. 我认为您一定有误解。 What are you trying to do? 你想做什么?

Assuming the method only has to return true or false if it is greater than equal to 3 , would recommend to keep it simple. 假设该方法仅在greater than equal to 3时才必须返回truefalse ,所以建议使其保持简单。

Also, Please Note: 另外,请注意:

  • Loop through the i=0 to i< rankHist.length incase the array contains less than 13 elements you will encounter an ArrayOutOfBoundException. 如果数组包含少于 13个元素 ,则将i=0 to i< rankHist.length循环i=0 to i< rankHist.length ,您将遇到ArrayOutOfBoundException.

  • If it contains more than 13 elements, the output might be incorrect. 如果它包含13个以上的元素,则输出可能不正确。

.

public boolean isThreeKind( int rankHist[]) {

 for (int i=0;i<rankHist.length;i++){
      if (rankHist[i]>=3){ 
          return true; 
      }
  }

  return false;
}

This is literally what your code is doing right now: 从字面上看,这就是您的代码现在正在执行的操作:

public boolean isThreeKind( int rankHist[]) {
    return rankHist[0] >= 3;
}

That is it, and I am assuming this is not what you are attempting to do. 就是这样,我假设这不是您要尝试执行的操作。 So if you tell us what you are actually trying to accomplish we can help you more. 因此,如果您告诉我们您实际要实现的目标,我们将为您提供更多帮助。

In java, You always gotta keep track of returning something EVERYWHERE the method can exit. 在Java中,您总是必须跟踪该方法可以退出的所有地方。 If it can exit without hitting a return statement, you'll see that error. 如果它可以退出而无需点击return语句,您将看到该错误。

So, for your code, to modify it and make it see it, you would need to have it say: 因此,对于您的代码,要对其进行修改并使其可见,您需要使它说:

public boolean isThreeKind( int rankHist[]) {
  for (int i=0;i<=13;i++){
      if (rankHist[i]>=3){
          return true;
      }else{
          return false;
      }

  }
return false;
}

Or true, if you would rather that. 或是的,如果您愿意的话。

You should avoid multiple return statement. 您应该避免使用多个return语句。
You can use a flag. 您可以使用标志。 Declare the flag outside the for loop, then assign the value accordingly, and do not miss the break statement . 在for循环外声明该标志,然后相应地分配值, 不要错过break语句
Do it like that: 那样做:

public boolean isThreeKind( int rankHist[]) {
  boolean value = false;
  for (int i=0;i<=13;i++){
      if (rankHist[i]>=3){
          value = true;
          break;
      }    
  }
  return value;
}

I think what he wants is if every element in the 13 are >=3, he should return a true, else ways he should return a false. 我认为他想要的是,如果13中的每个元素> = 3,他都应该返回true,否则应该返回false。

public boolean isThreeKind( int rankHist[]) 
{
  for (int i=0;i<=13;i++)
  {
      if (rankHist[i]<3)
      {
          return false; // Will return false if either of the element have value <3
      }
  }
  return true;  // Will return true only if all the 13 elements have value >=3
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM