Java stdin没有提示程序运行

时间:2017-02-22 13:47:16

标签: java eclipse stdin

我正在研究Merge-sort算法,我目前正在尝试测试它是否有效。

应该通过输入格式的stdin控制:

数组长度 元素值(以“”分隔)

但是,在输入时:

3
17 10 3

没有任何反应,点击进入,只需让我进入控制台的下一行。

我没有收到任何错误消息(除非输入错误的输入),所以我很难搞清楚为什么不提示脚本运行给定的输入。

(代码复制如下)

package merge;

import java.io.*;
import java.util.*;

public class MergeSort
{
    // This method takes two sorted arrays of integers as input parameters
    // and it should return one sorted array of integers.
    public int[] merge(int[] A1, int[] A2) {
        int[] C = new int[A1.length + A2.length];
        int i = 0, j = 0, k = 0;
        while (i < A1.length && j < A2.length)
            C[k++] = A1[i] < A2[j] ? A1[i++] :  A2[j++];

        while (i < A1.length)  
            C[k++] = A1[i++];

        while (j < A2.length)    
            C[k++] = A2[j++];

        return C;
    }

    // This method takes an array of integers as input parameter,
    // and it should then return the integers sorted
    // in ascending order using the MergeSort algorithm.
    private int[] sort(int[] numbers) {
        //pointers
        int i = 0, j = 0, k = 0;
        // reference values
        int half = numbers.length / 2;
        int[] sorted = new int[numbers.length];

        if (numbers.length <= 1){
            return numbers;
        }

        //left part of 'numbers'
        int[] left = new int[half];
        int[] right;

        //right part of 'numbers' 
        if (numbers.length % 2 != 0){
            right = new int[half+1];
            j = half + 1;
        }
        else{
            right = new int[half];
            j = half;
        }
        //fills out left half of array with values from input array
        while (i < half)
            left[i] = numbers[i];
            i++;

        //fills out right half of array with values from input array
        while (j < numbers.length)
            right[j] = numbers[k];
            j++; k++;

        left = sort(left);
        right = sort(right);

        merge(left,right);

        return sorted;
    }

    // ##################################################
    // # Stdin part. #
    // ##################################################

    public static void main(String[] args) throws IOException {
        new MergeSort().run();
    }

    private void run() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int[] numbers = readIntArray(in);

        numbers = sort(numbers);
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + " ");
        }
    }

    private int[] readIntArray(BufferedReader in) throws IOException {
        int length = Integer.parseInt(in.readLine());
        int[] array = new int[length];
        StringTokenizer st = new StringTokenizer(in.readLine());

        for (int i = 0; i < length; i++) {
            array[i] = Integer.parseInt(st.nextToken());
        }
        return array;
    }
}

1 个答案:

答案 0 :(得分:1)

您拨打readLine()两次,导致尝试在标准输入上读取两行。