Evens和赔率数组

时间:2016-01-22 17:42:25

标签: java arrays

我需要将一个数组转换为两个独立的数组,将赔率和均值分开。到目前为止我得到了这个,但我的输出不对。如果有人能帮助我解决这个问题,我将非常感激,因为它将在本周末结束时上课。

import java.util.Arrays;
import java.util.Scanner;

public class EvensAndOdds
{
private static int countEm(int[] array)
{
    int numOdd = 0; 
    for(int i=0; i< array.length; i++)
        if(array[i] % 2 == 1)  
            numOdd ++;

    return numOdd;
}
public static int[] getAllEvens(int[] array)
{
    int x = 0; 
    int[] evens  = new int[array.length - countEm(array)];
    for(int i=0; i<array.length; i++)
    {
        if(array[i]%2==0)
        {
            evens[x] = array[i];
            x ++; 
        }
    }
    return evens;
    }
public static int[] getAllOdds(int[] array)
{
    int y = 0;
    int[] odds = new int[countEm(array)];
    for (int i = 0; i < array.length; i++)
    {
        if (array[i] %2 == 1)
        {
        odds[y] = array[i];
        y++;
        }
    }
    return odds;
}

public static void main(String[] args)
{
    int[] input = {2,4,6,8,10,12,14};

    System.out.println(Arrays.toString(input));
    System.out.println("odds = " + EvensAndOdds.getAllOdds(input));
    System.out.println("evens =" + EvensAndOdds.getAllEvens(input));
}
}

输出结果如下:

[2, 4, 6, 8, 10, 12, 14]
odds = [I@659e0bfd
evens =[I@2a139a55

1 个答案:

答案 0 :(得分:0)

Arrays.toString(EvensAndOdds.getAllOdds(input)))