为什么我得到这个错误的输出?

用户10065560

https://www.hackerrank.com/challenges/java-arraylist/probleminput

5

5 41 77 74 22 44

1 12

4 37 34 36 52

0

3 20 22 33

5

1 3

3 4

3 1

4 3

5 5

样本输出

74

52

37

错误!

错误!

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

public class Solution {

public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    ArrayList array[] = new ArrayList[n];
    for(int i = 0; i < n; ++i)
    {
        ArrayList list = new ArrayList();
        int no = sc.nextInt();
        while(no != '\n')
        {
             list.add(no);
             no = sc.nextInt(); 
        }
        array[i] = list;
    }

    int k = sc.nextInt();
    int l = sc.nextInt();
    System.out.println(array[k].get(l));
}

}

线程“main”中的错误(stderr)异常 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 Solution.main(Solution.java:18)
卡米尔莱斯

如果我正确理解了这个问题,它应该首先使用 int 然后扫描 n 行并创建某种二维列表/数组,然后应该接受关于这个二维对象中的位置 (x,y) 的问题的 int作为“错误!”超出范围。

import java.util.ArrayList;
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
    /*
     * Enter your code here. Read input from STDIN. Print output to STDOUT.
     * Your class should be named Solution.
     */
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    sc.nextLine();
    ArrayList array[] = new ArrayList[n];
    for (int i = 0; i < n; i++) {
        ArrayList list = new ArrayList();
        Scanner linSc = new Scanner(sc.nextLine());
        while (linSc.hasNextInt()) {
            list.add(linSc.nextInt());
        }
        linSc.close();
        array[i] = list;
    }
    n = sc.nextInt();
    for (int i = 0; i < n; i++) {
        int k = sc.nextInt();
        int l = sc.nextInt();
            try {
                System.out.println(array[k - 1].get(l));
            } catch (IndexOutOfBoundsException e) {
                System.out.println("ERROR!");
            }
        }
        sc.close();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章