数组输出零而不是数字

时间:2017-02-14 16:13:45

标签: java arrays

代码应该输出有序数组,但它输出零。

例如,在运行程序时,写下" 2"当它要求方法时,然后" 5",当它要求计数和" 10" " 9" " 2" " 5" " 0",当它要求物品时。然后你会看到。我按照给出的所有说明进行操作,我似乎无法找到代码的问题,在输出时将输入的数字更改为零...

    public static void secondMethod(int[] a) {

        long t1 = System.nanoTime();

        int N = a.length;
        int b[] = new int[N];
        int c[] = new int[N];

        int[] tmp;
        int len = 1;

        for (int i = 0; i < a.length; i++) {
            a[i] = b[i];
        }

        while (len < N) {
            int n = 0;
            int i;
            int j;
            int ri;
            int rj;

            for (int k=0; k<b.length; k+=2*len){{
                n=k;
                i = k;
                j = k+len;

                if(k+len<N){
                    ri = k+len;
                }else{
                    ri = N;
                }
                if(k+2*len<N){
                    rj = k+2*len;
                }else{
                    rj = N;
                }
            }

            while(i<ri && j<rj){
                if(b[i]<b[j]){
                    c[n]=b[i];
                    i++;
                    n++;
                }else{
                    c[n]=b[j];
                    j++;
                    n++;
                }
            }
            while(i<ri){
                c[n]=b[i];
                i++;
                n++;
            }
            while(j<rj){
                c[n]=b[j];
                j++;
                n++;
            }
            }
            len = len*2;

            tmp = b;
            b = c;
            c = tmp;
        }

        for (int i = 0; i < a.length; i++) {
            a[i] = b[i];
        }

        long t2 = System.nanoTime();
        long t = t2 - t1;
        System.out.println("t=" + t);
    }
    public static void main(String[] args) {

        int nm, mtd;
        Scanner sc = new Scanner(System.in);

        System.out.print("method: ");
        if (sc.hasNextInt())
            mtd = sc.nextInt();
        else {
            System.out.println("input-output error");
            sc.close();
            return;
        }
        if (mtd != 1 && mtd != 2) {
            System.out.println("input-output error");
            sc.close();
            return;
        }
        System.out.print("count: ");
        if (sc.hasNextInt())
            nm = sc.nextInt();
        else {
            System.out.println("input-output error");
            sc.close();
            return;
        }

        int a[] = new int[nm];

        System.out.println("items: ");

        for (int i = 0; i < a.length; i++) {

            if (sc.hasNextInt())
                a[i] = sc.nextInt();
            else {
                System.out.println("input-output error");
                sc.close();
                return;
            }

        }
        sc.close();
        if (mtd == 1) {
            firstMethod(a);
            System.out.println("sorted: ");
            for (int i = 0; i < a.length; i++)
                System.out.print(a[i] + " ");
        }
        if (mtd == 2){
            secondMethod(a);
            System.out.println("sorted: ");
            for (int i = 0; i < a.length; i++)
                System.out.print(a[i] + " ");
        }

    }

1 个答案:

答案 0 :(得分:0)

在您的代码中,在secondMethod(int[] a)方法中,在第一个for循环中,使用

b[i] = a[i];

而不是

a[i] = b[i];

它应该有效。 下次尝试调试,这会有所帮助。