Java 试图打印奇数

Java 试图打印奇数,java,if-statement,data-structures,Java,If Statement,Data Structures,我有一个程序,要求用户输入正数。然后程序打印出用户输入,然后取出偶数并显示偶数。现在,当我试图显示赔率时,它会打印出偶数和奇数。。我有if语句if(oddNum%2!=0)//表示if甚至不执行以下操作。。我似乎无法理解我的错误 System.out.println("\n"); System.out.println("Here is the new even array: "); for(j=0; j<i;j++)

我有一个程序,要求用户输入正数。然后程序打印出用户输入,然后取出偶数并显示偶数。现在,当我试图显示赔率时,它会打印出偶数和奇数。。我有if语句if(oddNum%2!=0)//表示if甚至不执行以下操作。。我似乎无法理解我的错误

            System.out.println("\n");
            System.out.println("Here is the new even array: ");
            for(j=0; j<i;j++)
            {
                if (oddNum[j]%2 == 0)//if odd num equals zero separate the evens
                {
                    System.out.println("evenNum1:"+evenNum);
                evenNum[evenIndex]=oddNum[j];//sorts the even numbers to the next array
                evenIndex++;

                }
            }

            if(evenIndex !=0)
            {
            for(j=0;j<evenIndex;j++)
            {
                if(evenNum[j]>0)
                {
                    System.out.println(evenNum[j]);
                }
            }

            }
            System.out.println("\n");
            System.out.println("Here is the new odd array: ");
            for(b=0;b<i;b++)
            {
                if(oddNum[b]%2!=0 )//if odd
                {

                    oddNum[oddIndex]=evenNum[b];//sorts the input numbers to the next array
                    oddIndex++;



                }
            }
            if(oddIndex!=0)
            {
                for(b=0;b<oddIndex;b++)
                {
                    if(oddNum[b]>0)
                    {
                        System.out.println(oddNum[b]);
                    }
                }
            }
     }

 }
System.out.println(“\n”);
System.out.println(“这是新的偶数数组:”);

对于(j=0;j在奇数if循环中,您需要写入oddNum[odddindex]=oddNum[b]而不是oddNum[odddindex]=evenNum[b]

if(oddNum[b]%2!=0 )//if odd
        {
            oddNum[oddIndex]=oddNum[b];//sorts the input numbers to the next array
            oddIndex++;
         }

并确保oddIndex和evenIndex已初始化为0。