我试图弄清楚如何对一个数组中的输入列表进行排序,并从中制作两个不同的数组,无论是偶数还是奇数。我似乎无法将整数添加到 if 循环中的数组中。
这是我的代码:
Scanner in = new Scanner (System.in);
System.out.print("Enter a number");
int number = in.nextInt();
int [] x = new int[0];
int [] even = new int [0];
int [] odd = new int [0];
for (int i = 0; i <x.length; i++)
{
if (in.nextInt() == 0){
for (i = 0; i <x.length; i++)
{
if (x[i] % 2 == 0)
{
even = even + x[i];
System.out.print("Even numbers = " + even);
i++;
}
if (x[i] % 2 != 0)
{
odd = odd + x[i];
System.out.print("Odd numbers = " + odd);
i++;
}
}
break;
}
else {
x[i] = number;
i++;
}
}