简体   繁体   中英

I want to print odd numbers in an array using methods

This code print the array (1, 7, 8, 22, 37, 55, 80) the way it is without calculating its evens.

The output that I want (8, 22, 80).

The output that I get (1, 7, 8, 22, 37, 55, 80).

///The getEvens() method
public static int[] getEvens(int a[]) {

    int myEvens = 0;

    for (int i = 0; i < a.length; i++) {
      if (a[i] % 2 == 0) {
         myEvens++;
      }    
    }

    Arrays.sort(a);

    return a;
}

\\\\The main method
public static void main(String args[]) {

    int [] getEvens;
    int [] myArray = {1,8,80,22,55,37,7};
    int [] evenResult = getEvens(myArray);

    System.out.print("\nThe even numbers in the array(" +    Arrays.toString(myArray)+ ") are:{ " + Arrays.toString(evenResult)+ "}.");

}

In the getEvens method you do not make a new array out of the even numbers. All you do is count them. You need to change it to something like the following:

public static int[] getEvens(int a[]) {
    int myEvens = 0;

    for (int i = 0; i < a.length; i++) {
      if (a[i] % 2 == 0) {
         myEvens++;
      }    
    }

    //the code to actually add the evens to a new array
    int[] evenArray = new int[myEvens];
    int evenIndex = 0;
    for (int i = 0; i < a.length; i++) {
      if (a[i] % 2 == 0) {
        evenArray[evenIndex] = a[i];
        evenIndex++;
      }    
    }
    Arrays.sort(evenArray);

    return evenArray;
}

Here's a piece of code made in C++, but it's similar for Java:

int main(){
    int A[] = {1,8,80,22,55,37,7};
    int num = 0;
    for (int i=0; i < 7; i++){
        num = A[i];
        if (!(num & 1))
            printf("%d\n", num);
    }
    return 0; 
}

What it does is that we store the i th element on the array and we check the last bit on the number and comapre it with 1

For example:

We take the number 1 which in binary is 00000001, because all the positions in binary are power of 2 the only bit that can make the number odd or even is the last one which sums only 1 extra to the number. So in this case with 1 the result will give true , and we are looking for all the number that matches with 0 we negate the result with the matching with 1 .

The next number we take is 8 , in binary it's: 00001000

128 64 32 16 8 4 2 1 --(value in power of 2)

0 0 0 0 1 0 0 0 -----------(binary position)

0+0+0+0+8+0+0+0 = 8

And because our last bit is a 0 we know that the number is a even number 100%, so our if:

if(!(num & 1))

Will be false, but because we have our not ( ! ) it will go through the if and print the number.

Your output should be as you describe even though (8, 22, 80) are even numbers and you're asking for printing odd which are the rest of the numbers.

If you're looking for the odd numbers (1, 55, 37, 7) you just need to change the if to make the result true when it does match the last bit with 1 :

if(num & 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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