简体   繁体   English

如何使该程序对数组中的奇数和偶数进行计数?

[英]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 例如,如果键入10,它将在0-999的时间间隔之间打印出10个随机数,然后将奇数和偶数分开

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'' 在最后一个阶段,它是要计算有多少个奇数和偶数,例如“ 10个数字中有4个是偶数而6个是奇数”

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: 为什么不只使用按位AND并删除这些条件,如下所示:

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);

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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