如何在数组中查找奇数和偶数? [英] How to find the Odd and Even numbers in an Array?

查看:161
本文介绍了如何在数组中查找奇数和偶数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在尝试查找数组的奇数和偶数.这是到目前为止的代码.我知道我的findEvens()和findOdds()方法很混乱,因为每当我尝试打印最终结果时,它们都会不断给我提供值.例如,如果我尝试找到{1,5,8,3,10}的赔率,它会给我{5,3,0}.而且,如果我尝试找到{2,5,8,7,19}的偶数,它会给我{2,8,0}.有人知道为什么吗?

Right now, I'm trying to find the odd and even numbers of an array. Here's the code of what I have so far. I know my findEvens() and findOdds() methods are messed up because they keep giving me off values whenever I try to print the final result. For example, if I try to find the odds of {1,5,8,3,10}, it gives me {5,3,0}. And if I try to find the evens of {2,5,8,7,19}, it gives me {2,8,0}. Anyone know why?

public class Scores {
   private int[] numbers;
   public Scores(int[] numbersIn) {
      numbers = numbersIn;
   }
   public int[] findEvens() {
      int numberEvens = 0;
      for (int i = 0; i < numbers.length; i++) {
         if (i % 2 == 0) {
            numberEvens++;
         }
      }
  int[] evens = new int[numberEvens];
  int count = 0;
  for (int i = 0; i < numbers.length; i++) {
     if (numbers[i] % 2 == 0) {
        evens[count] = numbers[i];
        count++;
     }      
  }      
  return evens;
}
public int[] findOdds() {
  int numberOdds = 0;
  for (int i = 0; i < numbers.length; i++) {
     if (i % 2 == 0) {
        numberOdds++;
     }
  }
  int[] odds = new int[numberOdds];
  int count = 0;
  for (int i = 1; i < numbers.length; i++) {
     if (numbers[i] % 2 == 1) {
        odds[count] = numbers[i];
        count++;
     }      
  }      
  return odds;
 }
 public double calculateAverage() {
      int sum = 0;
      for (int i = 0; i < numbers.length; i++) {
         sum += numbers[i];
      }   
      return (double) sum / numbers.length;
   }
 public String toString() {
    String result = "";
    for (int i = 0; i < numbers.length; i++) {
       result += numbers[i] + "\t";
    }
    return result;
 }
public String toStringInReverse() {
  String result = "";
  for (int i = numbers.length - 1; i >= 0; i--) {
     result += numbers[i] + "\t";
  }
  return result;
  }
}                  

推荐答案

您的问题是计算自己有多少个偶数

You're problem is in counting how many even numbers you have

public int[] findEvens() {
  int numberEvens = 0;
  for (int i = 0; i < numbers.length; i++) {
     if (i % 2 == 0) {
        numberEvens++;
     }
  }

这将始终返回一个数字,该数字是数字长度的一半,因为您正在对数组中的元素数量进行mod划分,而不是对元素本身进行mod划分.将numbers[i]添加到if语句

this will always return a number that is half the size of the length of numbers because you're doing mod division on the number of elements in the array, not on the elements themselves. Add numbers[i] to the if statement

public int[] findEvens() {
  int numberEvens = 0;
  for (int i = 0; i < numbers.length; i++) {
     if (numbers[i] % 2 == 0) {
        numberEvens++;
     }
  }

看起来您在奇数计数时遇到了同样的问题

looks like you've got the same problem with odd count

这篇关于如何在数组中查找奇数和偶数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆