扫描仪没有正确输入

时间:2017-09-12 12:17:32

标签: java

 public static void main(String[] args) {
    int T = sc.nextInt();
    for (int i = 1; i <= T; i++) {
        int xs ;
        int n = sc.nextInt();
        int divisor = 1;
        for (int j = 0; j < n -1;j++){
            divisor = divisor * 10;
        }
        //System.out.println(divisor + "/" + n);

        int[][] matrix = new int[n][n];
        int[][] matrix2 = new int[n][n];

        for (int k = 0; k < n; k++) {
             int d2  = divisor;
             xs = sc.nextInt();
             System.out.println(xs);
           // System.out.println("\n"+xs + "df" + xs.length());
            for (int l = 0; l < n; l++) {
                matrix[k][l] =  xs / d2;
                matrix2[k][l] = 0;
                xs = xs % d2;
                divisor = d2/10;
            }
        }
        // System.out.println("Hello");
        matrix2[0][0] = 1;
        Node node = new Node(0, 0);
        node.dis = 0;
        node.steps = 0;
        node.c = 0;
        openList[0] = node;
        util_expl(node, matrix, matrix2, n);
        fun(matrix, matrix2, n);
        System.out.println(res);
    }
}//

我的输入例如是

1
4
0100
1110
1011
1010
但我的扫描仪因为无法正确输入而卡住了。我立刻粘贴整个输入 我打印线来检查输入是否正确,但答案是这样的 100个
1110
1010
然后就会卡住。

1 个答案:

答案 0 :(得分:2)

因为您从 1 开始并且您正在使用nextInt();

工作示例:

Scanner sc = new Scanner(System.in);
        System.out.println("type how many values : ");
        int t = sc.nextInt();
        String []a = new String[t] ;
        for (int i = 0; i < t; i++) 

            a[i] = sc.next();

            for (String item : a)
                System.out.println(item);

enter image description here