传输代码时出错

时间:2016-11-23 17:03:43

标签: java

我正在为类编写代码,当我在课堂上运行Java博士时,代码工作正常。但是当我输入它进行评分时,我收到的错误是:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Main.main(Main.java:247)
    at Ideone.assertRegex(Main.java:94)
    at Ideone.test(Main.java:42)
    at Ideone.main(Main.java:29)

我不知道这意味着什么,我们还没有涵盖这类事情,我不是一个非常有经验的程序员,对不起。我的代码如下;

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int indexFirst = 0;
        int indexSecond = 0;
        int[] first = new int[10000];
        int[] second = new int[10000];

        System.out.println("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
        do {int value = scanner.nextInt();
            if (value < 0) {
                break;
            }
            first[indexFirst++] = value;
        } while(true);

        System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");
        do {int value = scanner.nextInt();
            if (value <= 0) {
                break;
            }
            second[indexSecond++] = value;
        } while(true);
        System.out.println("First Array:");
        for (int i = 0; i < indexFirst; i++) {
            System.out.print(first[i] + " ");
        }
        System.out.println("\n");

        System.out.println("Second Array:");
        for (int i = 0; i < indexSecond; i++) {
            System.out.print(second[i] + " ");
        }
        System.out.println("\n");
        for (int i = 1; i < indexFirst; i++) {
            if (first[i-1] > first[i] ) {
                System.out.println("ERROR: Array not in correct order");
                return;
            }
        }

        for (int i = 1; i < indexSecond; i++) {
            if (second[i-1] > second[i] ) {
                System.out.println("ERROR: Array not in correct order");
                return;
            }
        }

        int[] merged = new int[indexFirst + indexSecond];
        int curIdx1 = 0;
        int curIdx2 = 0;
        for(int mergedIdx = 0; mergedIdx < merged.length; mergedIdx++) {
            if (curIdx2 == indexSecond) {
                merged[mergedIdx] = first[curIdx1++];
            } else if (curIdx1 == indexFirst) {
                merged[mergedIdx] = second[curIdx2++];
            } else if (first[curIdx1] < second[curIdx2]) {
                merged[mergedIdx] = first[curIdx1++];
            } else {
                merged[mergedIdx] = second[curIdx2++];
            }
        }
        System.out.println("Merged Array:");
        for (int i = 0; i < merged.length; i++) {
            System.out.print(merged[i] + " ");
        }
    }
}

如果有人对如何解决此问题有任何意见,我们将不胜感激。

0 个答案:

没有答案