从原始数组中排序偶数

时间:2016-03-09 22:41:33

标签: java arrays

我必须创建一个程序,从用户读取任意数量的正整数并将它们存储到数组中。输入数据的数量不超过100.在用户完成程序后,应删除所有偶数整数并将它们放在另一个数组中,使原始数组中的所有奇数整数都没有空洞。 它应该显示原始数组中的内容作为输入的顺序,带有计数的偶数整数数组的内容,以及取出所有带计数的偶数后的原始数组的内容

不应使用第三个数组

在取出偶数整数后,我无法显示原始数组和原始整数数组。这是我到目前为止的代码

import java.util.*;

public class Arrays
{

public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter in any amount of positive numbers, Enter -1 when finished");

    int i=0;
    int nextElm=0;
    int a,b;

    int[] origArray = new int[100]; /* Two arrays at length 100*/
    int[] evenArray = new int[100];

    while((i<origArray.length && i<evenArray.length)&& nextElm!= -1)
    {
        System.out.println("Enter next number: ");
        nextElm = scan.nextInt();

        if (nextElm%2 != 0)//Sorts even numbers
        {
            origArray[i]= nextElm;
        }
        else
            evenArray[i] = nextElm;
            i++;

    }



    System.out.print("\n");

    System.out.println("Even Array: ");

    for (b=0; b<evenArray.length;b++)
    {
        if (evenArray[b]== -1)
        {
            evenArray[b]= 0;
        }
        if(evenArray[b]!= 0)
        {
            System.out.print(evenArray[b]+" ");
        }
    }

    System.out.print("\n");
    System.out.println("Original Array: ");

            for(a=0; a<origArray.length && a<evenArray.length; a++)
            {
                if (origArray[a]== -1)
                {
                    origArray[a]= 0;
                }
                if(origArray[a]!= 0)
                {
                    System.out.print(origArray[a]+" " + evenArray[a]);
                }
    }


    System.out.print("\n");

}
}

2 个答案:

答案 0 :(得分:0)

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter in any amount of positive numbers, Enter -1 when finished");

    int i = 0;
    int nextElm = 0;
    int a, b;

    int[] origArray = new int[100]; /* Two arrays at length 100 */
    int[] evenArray = new int[100];

    while (nextElm != -1) {
        System.out.println("Enter next number: ");
        nextElm = scan.nextInt();
        if (nextElm > 0) {
            origArray[i] = nextElm;
            i++;
        }

    }

    int x = 0;

    System.out.println();

    // Displays original array and sorts even numbers to even array +
    // original count
    System.out.printf("\nTotal count of original array is : %d", i);
    System.out.println();
    for (int orgNumber : origArray) {
        if (orgNumber != 0) {
            System.out.print(orgNumber + " ");
        }
        if (orgNumber % 2 == 0) {
            if (orgNumber != 0) {
                evenArray[x] = orgNumber;
                x++;
            }
        }
    }

    System.out.println();

    // Displays sort even numbers to even array + even count
    System.out.printf("\nTotal count of even array is : %d", x);
    System.out.println();
    for (int evenNumber : evenArray) {
        if (evenNumber != 0) {
            System.out.print(evenNumber + " ");
        }
    }

    // Displays sort odd numbers from original array + odd count
    System.out.printf("\nTotal count of orignal array without even is : %d", i - x);
    System.out.println();
    for (int oddNumber : origArray) {
        if (oddNumber % 2 != 0) {
            System.out.print(oddNumber + " ");
        }
    }

}

如果我理解你的问题,这应该是完美的。希望你觉得这很有帮助。

答案 1 :(得分:0)

你想要三个独立的循环。一个用于输入值(我建议您使用while构造)和一个用于对值进行排序。

您的输入循环每次都应该以新的数字读取,并且只在用户想要时退出。例如,如果用户输入负数,那么您的循环将退出。像这样:

System.out.println("Enter any number of numbers; enter a negative when finished.");
int nextElm = 0;
int count = 0;
while (nextElm >= 0) {
    nextElm = scan.nextInt();
    origArray[count] = nextElm;
    count++;
}
// This is where you would put a print statement that prints the original array with a count.

此后,使用java.util.Arrays.copyOf()方法修剪数组的空元素,如下所示:

origArray = java.util.Arrays.copyOf(origArray, count+1);

接下来,使用for循环遍历数组,并将偶数移动到另一个数组。这是棘手的部分,因为从数组中删除项需要迭代将每个值移动到它的先前索引。

int j = 0;
for (int i=0; i<count; i++){
    if (origArray[i] % 2 == 0){
        evenArray[j] = origArray[i]; //add even number to evenArray
        j++;                         //move to next index of evenArray
        for (int k=i; k<(origArray.length-1); k++){
            origArray[k] = origArray[k+1]; //store next value in current index
        }
    }
}

然后最后,使用java.util.Arrays.copyOf()方法再次删除数组的空元素,如下所示:

evenArray = java.util.Arrays.copyOf(evenArray, j+1);

免责声明:这些都是从臀部编码的,所以如果我错过了某些内容或做错了什么,请告诉我。