简体   繁体   中英

How to make this programm to count the odd and even numbers in an array?(eclipse)

Thanks for the answers guys, didn't expect getting answers so fast.

Ok so in this code at the final stage it is meant to count how many odd and evens numbers there are in the array length you decide.

If you for example type in 10 it prints out 10 random numbers between the intervall of 0-999 and then it seperates the odd and even numbers

In the last stage it's meant to calculate how many odd and even numbers there are like ''out of the 10 numbers 4 of them were even numbers and 6 were odd numbers''

Right now in the last stage it just prints out numbers randomly and doesen't calculate how many odd and even numbers there are. I don't know how to fix it.

I have ran out of ideas about it so hopefully someone here can make it work properly.

import java.util.Scanner;
public class Uppgift4 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
            int length;
    while (true)
    {
             System.out.print(" \n\nHow many numbers do you want in the intervall 0-999?(turn off with -1 or 1000):");
             length = scan.nextInt();
              if (length>999)
              {
                System.out.print("\nValue outside intervall restart programm");
              break;
              }
              if (length<0)
              {
                System.out.print("\nValue outside intervall restart programm");
              break;
              }
             System.out.println("\n Here are the random numbers:"); 
            int [] ar1 = new int[length];
            for(int i = 0; i <  length; i++) {
                ar1[i] = (int)(Math.random() * 1000);


        {
            System.out.print(" "+ar1[i]);

        }
            }
            System.out.println(" \n");
            System.out.println(" Here are the numbers divided between even and odd numbers:");

        System.out.print(" ");
        for(int i = 0 ; i < length ; i++)
        {
            if(ar1[i] % 2 == 0)
            {
                System.out.print(ar1[i]+" ");
            }
        }

        System.out.print("- ");
        for(int i = 0 ; i < length ; i++)
        {
            if(ar1[i] % 2 != 0)
            {
                System.out.print(ar1[i]+" ");
            }
        }
        System.out.println(" \n");
        System.out.print(" Of the above numbers "+ length + " so  ");

    System.out.print("where ");
    for(int evennumbers = 1 ; evennumbers < length ; evennumbers++)
    {
        if(ar1[evennumbers] % 2 == 0)
        {
            System.out.print(evennumbers+" ");
        }
    }

    System.out.print(" of the numbers even and odd numbers were ");
    for(int oddnumbers = 1 ; oddnumbers < length ; oddnumbers++)
    {
        if(ar1[oddnumbers] % 2 != 0)
        {
            System.out.print(oddnumbers+" ");
        }
        }
    }   
    }

You need to count the number of even and odd number:

int even = 0;
int odd = 0;

// Loop through the final array
for(int i = 0 ; i < length ; i++)
{
    if(ar1[i] % 2 == 0)
    {
       even++;
    } else {
       odd++;
    }
}

Even simpler:

for(int i = 0 ; i < length ; i++)
{
    odd += (ar1[i] % 2)
}
even = length - odd;

just make two global variables to count the odd and even and put them into the condition where you are checking for odd and even. code

Why not just make use of bitwise AND and remove those conditionals like so:

int odd = 0;
int even = 0;
for(int i=0;i<length;i++){
    odd+=ar1[i]&1;
    even+=((ar1[i]+1)&1);
}

you can use this way , very simple

public static void main(String []args){
             Integer[] arr = new Integer[] { 1,2,3,4,5};
             int oddCount =0; int evenCount=0;
    for ( int i=0; i< arr.length ;i++){
               if(  ( arr[i] % 2) == 0 )
                  evenCount++;
              if( arr[i] % 2 != 0 )
                  oddCount++;
    }

    System.out.println( "oddCount in Array  :" + oddCount + " EvenCount in Array : " + evenCount);

 }

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