尝试从用户输入中读取2D int数组时发生运行时错误

问题描述 投票:-3回答:1

我编写的要在二维数组中扫描的代码可以在下面看到,我在运行时遇到问题,这是输出:

Runtime Error

Exception in thread "main" java.util.InputMismatchException: For input string: "00000000100000000000"

at java.util.Scanner.nextInt(Scanner.java:2123)

at java.util.Scanner.nextInt(Scanner.java:2076)

at Main.main(Main.java:17)

代码:

import java.util.Scanner;

public class Main 
{
    public static void main(String args[]) 
    {
        final int m = 20; // Rows
        final int n = 20; // Columns
        Scanner s1 = new Scanner(System.in);
        int num = s1.nextInt();
        int myArray1[][] = new int[m][n]; 

        for (int i = 0; i < m; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                myArray1[i][j] = s1.nextInt();
            }
        }

        for (int i = 0; i < m; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                System.out.print(myArray1[i][j] + "");
            }
        }
    }
}

如果有人可以帮助我,我将非常感激!

此输入为:

00000000000000000000
00000000000000000000
00000000010000000000
00000000010000000000
00000000000000000000
00000000000000000000
java arrays java.util.scanner
1个回答
0
投票

您没有以正确的方式使用扫描仪。

© www.soinside.com 2019 - 2024. All rights reserved.