按奇数或偶数顺序排列数组

时间:2017-04-20 22:22:33

标签: java arrays

我正在进行基础java编程的在线课程,我的老师给我分配了一个任务,让用户选择一些随机数,这些随机数将出现在屏幕上,然后进入一个数组,然后是那个数组必须被复制,然后该数组必须在另一个数组中按偶数/奇数排序,然后必须在屏幕上打印。

例如 用户将选择10个数字出现在屏幕上。 让我们说以下10个数字。 586 357 36 843 93 980 902 982 909 998 然后在下面一行说它应该说 586 36 980 902 982 998 - 357 843 93 909 在最后一行有多少偶数/不均匀数字在哪里 有6个偶数和4个不均匀的数字。

但我不知道如何在我的代码中将数组元素放到正确的位置。

import java.util.Arrays;     import java.util.Scanner;

public class Assignment 4
{

    public static void main(String[] args)
    {
        final int LENGTH = 1000;
        int[] values = new int[LENGTH];
        int currentSize = 0;
        int quantity = 0;
        double randomnumber = 0;
        int odd = 0;
        int even = 0;


        System.out.print("How many random numbers between 0 - 999 do you want?");   
        Scanner scan = new Scanner(System.in);
        quantity = scan.nextInt();
        while (quantity > currentSize && currentSize < values.length )
        {
            randomnumber = (Math.random() * 1000);
            int rounded = (int) Math.round(randomnumber);
            values[currentSize] = rounded;
            currentSize++;
        }

        System.out.print("\nHere are the randomnumbers: ");
        for (int i = 0; i < currentSize; i++)
        {
            System.out.print(values[i]);
            System.out.print(" ");
        }
        int[] values2 = Arrays.copyOf(values,values.length);
        int[] values3 = new int [quantity];
        System.out.print("\nHere are the randomnumbers in order: ");
        for(int i = 0; i < currentSize; i++)
        {

            if(values2[i] %2 == 0)
            {
                values3[i] = values2[i];    
            }

            if(values2[i] %2 == 0)
            {
                even++;
            }
            else
            {
                odd++;
            }
        }


        for (int i = 0; i < currentSize; i++)
        {
            System.out.print(values3[i]);
            System.out.print(" ");
        }   
        System.out.print("\nIt was " + even);
        System.out.print(" even numbers, and ");
        System.out.print(odd);
        System.out.print(" odd numbers");
    }
}

我不能使用Arraylist或任何其他方法。

我现在编码还没有完成,但是如果有人可以帮我把第二个数组中的元素放在第三个数组中的正确顺序中。

2 个答案:

答案 0 :(得分:1)

你走了:

int[] values = new int[]{586,357, 36, 843, 93, 980, 902, 982, 909, 998};
int[] odds = new int[values.length];
int[] evens = new int[values.length];
int oddCount = 0, evenCount = 0;
for(int number : values){
    if(number % 2 != 0){
        odds[oddCount++] = number;
    }else{
        evens[evenCount++] = number;
    }
}

System.out.println("Odd numbers : " + oddCount);
for(int i = 0 ; i < oddCount ; i++){
    System.out.print(odds[i] + " ");
}

System.out.println("Even numbers : " + evenCount);
for(int i = 0 ; i < evenCount ; i++){
    System.out.print(evens[i] + " ");
}

答案 1 :(得分:0)

通过稍微修改代码,您可以进行另一种近似:

int[] values2 = Arrays.copyOf(values, values.length);
int[] values3 = new int[quantity];
int temp = 0;
System.out.print("\nHere are the random numbers in order: ");
for (int value = 0; value<2 ; value++){
    for(int i = 0; i < currentSize; i++)
    {
        if(values2[i] %2 == 0)
        {
            if(value==0){
                values3[temp] = values2[i];
                temp++;
                even++;
            }
        }else{
           if(value==1){
                values3[temp] = values2[i];
                temp++;
                odd++;
            }
        }
    }
}

for (int i = 0; i < even; i++) //iterate over the first part of the array with even numbers
{
    System.out.print(values3[i]);
    System.out.print(" ");
}   
System.out.print("- "); //Add the separator at the end of all the even numbers
for (int i = even; i < currentSize; i++) //iterate over the last part of the array with odd numbers
{
    System.out.print(values3[i]);
    System.out.print(" ");
}   
System.out.print("\nIt was " + even);
System.out.print(" even numbers, and ");
System.out.print(odd);
System.out.print(" odd numbers");

我建议您使用System.out.format来获得更紧凑的代码,例如在代码的最后部分,而不是执行:

System.out.print("\nIt was " + even);
System.out.print(" even numbers, and ");
System.out.print(odd);
System.out.print(" odd numbers");

你可以这样做:

System.out.format("\nIt was %d even numbers and %d odd numbers", even, odd);

或简单地说:

System.out.print("\nIt was " + even + " even numbers and " + odd + " odd numbers");

甚至:

System.out.printf("\nIt was %d even numbers and %d odd numbers", even, odd);