将以空格分隔的用户输入加载到数组

时间:2017-04-20 05:39:47

标签: java arrays

我是Java的新手。我花了好几个小时试图弄清楚为什么这段代码不起作用。它应该采用由空格分隔的用户输入整数,并将它们加载到int数组中。然后,数字应该按降序排序,重复输入的频率必须显示在一个单独的int数组中。

我在输出中得到了零。任何人都可以帮我解决这个问题吗?

这就是我所拥有的:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] inputNumbers = new int[50];
    int[] ocurrences = new int[50];
    int size = 0;

    System.out.println("How many numbers to enter? (At most 50)");
    size = input.nextInt();

    System.out.println("Enter eaach of the numbers. Please put a space between each number.");
    String arrayString = input.next();
    input = new Scanner(arrayString);

    while(input.hasNextInt())
    {
        int nextNumber = input.nextInt();
        if(!isInArray(inputNumbers, size, nextNumber))
        {
            inputNumbers[size] = nextNumber;
            ocurrences[size] = 1;
            size++;
        }
        else
        {
            int index = search(inputNumbers, size, nextNumber);
            ocurrences[index]++;
        }
    }

    sort(inputNumbers, ocurrences, size);
    System.out.println("N\t\tCount");
    for(int i = 0; i < size; i++)
    {
        System.out.println(inputNumbers[i] + "\t\t" + ocurrences[i]);
    }
}

public static void sort(int[] inputNumbers, int[] ocurrences, int size)
{
    for(int current = 0; current < size; current++)
    {
        int max = current;
        for(int i = current; i < size; i++)
        {
            if(inputNumbers[i] > inputNumbers[max])
                max = i;
        }
        int temp = inputNumbers[max];
        inputNumbers[max] = inputNumbers[current];
        inputNumbers[current] = temp;

        temp = ocurrences[max];
        ocurrences[max] = ocurrences[current];
        ocurrences[current] = temp;
    }
}

public static int search(int[] array, int size, int number)
{
    for(int i = 0; i < size; i++)
    {
        if(array[i] == number)
            return i;
    }
    return -1;
}

public static boolean isInArray(int[] array, int size, int number)
{
    for(int i = 0; i < size; i++)
    {
        if(array[i] == number)
            return true;
    }
    return false;
}

}

目前的输出是:

How many numbers to enter? (At most 50)
4
Enter eaach of the numbers. Please put a space between each number.
5 5 4 7 8
N       Count
5       1
0       0
0       0
0       0
0       0
BUILD SUCCESSFUL (total time: 11 seconds)

应该是

之类的东西
N    Count
8    1
7    1
5    2
4    1

1 个答案:

答案 0 :(得分:1)

初始化我们知道确切大小的数组,以避免不必要的内存利用率。

input.next()也只会读取下一个完整的标记而不是整行。那是代码中的错误。

最后扫描仪资源未关闭。

我已经重构了您的代码,请检查一下。

public static void main(String[] args) {
        Scanner input = null;
        try {
            input = new Scanner(System.in);
            int[] inputNumbers, ocurrences;
            int index = 0, size;

            System.out.println("How many numbers to enter? (At most 50)");
            size = input.nextInt();
            inputNumbers = new int[size];
            ocurrences = new int[size];

            System.out.println("Enter each of the numbers. Please put a space between each number.");

            for (int i = 0; i < size; i++) {
                int nextNumber = input.nextInt();
                if (!isInArray(inputNumbers, nextNumber)) {
                    inputNumbers[index] = nextNumber;
                    ocurrences[index] = 1;
                    index++;
                } else {
                    int oIndex = search(inputNumbers, nextNumber);
                    ocurrences[oIndex] ++;
                }
            }

            sort(inputNumbers, ocurrences);
            System.out.println("N\t\tCount");
            for (int i = 0; i < index; i++) {
                System.out.println(inputNumbers[i] + "\t\t" + ocurrences[i]);
            }
        } finally {
            input.close();
        }
    }

    public static void sort(int[] inputNumbers, int[] ocurrences) {
        int size = inputNumbers.length;
        for (int current = 0; current < size; current++) {
            int max = current;
            for (int i = current; i < size; i++) {
                if (inputNumbers[i] > inputNumbers[max])
                    max = i;
            }
            int temp = inputNumbers[max];
            inputNumbers[max] = inputNumbers[current];
            inputNumbers[current] = temp;

            temp = ocurrences[max];
            ocurrences[max] = ocurrences[current];
            ocurrences[current] = temp;
        }
    }

    public static int search(int[] array, int number) {
        int size = array.length;
        for (int i = 0; i < size; i++) {
            if (array[i] == number)
                return i;
        }
        return -1;
    }

    public static boolean isInArray(int[] array, int number) {
        int size = array.length;
        for (int i = 0; i < size; i++) {
            if (array[i] == number)
                return true;
        }
        return false;
    }