2016-02-27 58 views
-1

我有一个程序,要求用户输入正数。然后该程序打印出用户输入,然后把它排除并显示平均值。现在,当我尝试显示赔率时,它打印出奇数和偶数..我有if语句if(oddNum%2!= 0) //表示如果不执行以下操作......似乎无法找出我的错误。试图让奇数打印

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

} 

回答

1

在你奇怪,如果循环你需要写oddNum [oddIndex] = oddNum [B]不oddNum [oddIndex] = evenNum [B]

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

,确保oddIndex和evenIndex初始化为0 –

+0

我的天啊,我感觉真的很愚蠢3小时的人谢谢 – Ronaldo

+0

很高兴我能帮上忙。 –