CCC 2018 J4:NoSuchElementException错误(Java)

时间:2019-02-20 05:18:43

标签: java exception

为了解决CCC(加拿大计算机竞赛)问题,我一直在运行此算法。它运行良好并在IntelliJ上给出正确的输出,但在DMOJ和CCC在线平地机上显示NoSuchElementException。

这是我的代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        solution(sc.nextInt());

    }

    public static void solution(int lines) {
        Scanner sc = new Scanner(System.in);
        int[][] sunflowers = new int[lines][lines];
        int[][] temp = new int[lines][lines];

        // Creating sunflowers array
        for (int i = 0; i < lines; i++) {
            for (int j = 0; j < lines; j++) {
                sunflowers[i][j] = sc.nextInt();

            }

        }

        boolean readyToSubmit = false;
        int a = 0;
        int b = 0;

        while (readyToSubmit == false) {
            b = 0;
            a = 0;

            readyToSubmit = true;
            for (int g = 0; g < sunflowers.length - 1; g++) {
                for (int h = 1; h < sunflowers[g].length; h++) {
                    if (sunflowers[g][h - 1] > sunflowers[g][h]) {
                        // Turn true if previous value smaller than current
                        readyToSubmit = false;
                    }
                }
            }

            // If each column is in descending order
            for (int d = 0; d < sunflowers.length; d++) {
                for (int e = 1; e < sunflowers.length; e++) {
                    if (sunflowers[e - 1][d] > sunflowers[e][d]) {
                        readyToSubmit = false;
                    }
                }

            }

            if (readyToSubmit == true) {
                break;
            }


            // Rotating the Array w/ temp
            for (int i = sunflowers.length - 1; i >= 0; i--) { // we want position to go right -> left
                b = 0;
                for (int j = 0; j < sunflowers[0].length; j++) { // We want columns to go up -> down

                    temp[a][b] = sunflowers[j][i];



                    b += 1;

                }
                a += 1;
            }

            for (int x = 0; x < lines; x++) {
                for (int y = 0; y < lines; y++) {
                    sunflowers[x][y] = temp[x][y];

                }
            }

        }

        for (int s = 0; s < sunflowers.length; s++) {
            for (int k = 0; k < sunflowers[s].length; k++) {
                System.out.print(sunflowers[s][k] + " ");
            }
            System.out.println();
        }

    }
}

输入: 3 3 7 9 2 5 6 1 3 4

输出(在IntelliJ中):

1 2 3
3 5 7
4 6 9

输出(在DMOJ上): IR(java.util.NoSuchElementException)

输出(在CCC分级机上):

Exception in thread "main" java.util.NoSuchElementException
<251 more characters> // unfortunately, I am not able to see what the 251 characters are.

我目前不确定是什么原因导致了NoSuchElementException(因为它没有告诉我DMOJ或CCC分级机上的行号)。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

注意:这是在评论部分中找到的,我只是将其添加为答案以验证此问题已得到解决。

Scanner sc = new Scanner(System.in);方法上删除此行solution。然后在sc之后关闭solution(sc.nextInt());行上的主要方法。请参阅此[link] 1 通过solution方法通过扫描仪。更改您的solution方法以接受扫描仪,因此方法签名将为public static void solution(int lines, Scanner sc),然后通过solution(sc.nextInt(), sc);在您的主方法中调用它。然后在solution(sc.nextInt(), sc);之后,使用sc.close()

关闭扫描仪