java 方法返回整数数组中奇数的个数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16289055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:31:52  来源:igfitidea点击:

method returns the number of odd numbers in the array of integers

javaarraysalgorithm

提问by Haider

I get an error message saying:

我收到一条错误消息说:

This method must return a result of type int.

此方法必须返回 int 类型的结果。

My code:

我的代码:

public int countOdds (int[] values) {
  int countOdd =0;
  for (int i=0; i<values.length; i++) {
    if (values[i] %2 != 0) {
      countOdd++;
      return countOdd;
    }
  }
}

回答by iluxa

Compiler thinks that the method may not return anything at all:

编译器认为该方法可能根本不返回任何内容:

public int countOdds (int[] values) {
  int countOdd =0;
  for (int i=0; i<values.length; i++) {
    if (values[i] %2 != 0){
      countOdd++;
      return countOdd;
    }
  }
  // what if this line is reached?
}

Suppose you run it with values.length = 0. What will the method return?

假设您使用values.length = 0. 该方法将返回什么?

Your returnstatement should be in a different place in code

您的return语句应该在代码中的不同位置

回答by Sam I am says Reinstate Monica

The following line

以下行

return countOdd;

should be outside of your for-loop.

应该在你的 for 循环之外。

Otherwise, you'll either return 1 when you reach the first odd, or you won't reach a return statement at all if there are no odds.

否则,您要么在达到第一个奇数时返回 1,要么在没有几率时根本不会到达 return 语句。

public int countOdds (int[] values) {
  int countOdd =0;
  for (int i=0; i<values.length; i++) {
    if (values[i] %2 != 0) {
      countOdd++;
    }
  }
  return countOdd;
}

回答by James Waldby - jwpat7

If the valuesarray has no odd numbers in it, then your function exits without returning an int. As noted in a previous answer, move the returnstatement out of the loop.

如果values数组中没有奇数,则您的函数退出而不返回int. 如上一个答案所述,将return语句移出循环。

Also, because the value of values[i]%2is 0 for even numbers and 1 for odd numbers, the number of odd numbers is equal to the sum over iof values[i]%2, allowing you to significantly shorten the code:

此外,由于值values[i]%2是0的偶数和1奇数,奇数的个数等于求和ivalues[i]%2,让您显著缩短了代码:

public int countOdds (int[] values) {
  int i, count=0;
  for (i=0; i<values.length; ++i) {
    count += values[i]%2;
  }
  return count;
}

An alternative version uses bitwise AND:

另一个版本使用按位 AND:

 public int countOdds (int[] values) {
  int i, count=0;
  for (i=0; i<values.length; ++i) {
    count += values[i] & 1;
  }
  return count;
}

回答by qwikLup

Supposing the if-statement never returns true, will the method return anything? No, it won't, so you need to include a return statement that will execute in the event the if-statement never runs.

假设 if 语句永远不会返回 true,该方法会返回任何内容吗?不,它不会,因此您需要包含一个 return 语句,该语句将在 if 语句永远不会运行的情况下执行。

  public int countOdds (int[] values) {
  int countOdd =0;
  for (int i=0; i<values.length; i++) {
    if (values[i] %2 != 0) {
      countOdd++;
    }
  }
  return countOdd;
}