InputMismatchException(原因未知)

时间:2019-03-13 15:34:06

标签: java java.util.scanner

我已经完成了这项作业,并且编写了我认为在逻辑上正确的代码。但是,当我编译它并提供测试用例时,它给了我InputMismatchException,并且似乎出现在第一个"SIT"的行上,其中的错误来自于随后的int我无法在更简单的程序上重现该错误。有人可以建议这里有什么问题吗?如果需要更多信息,请告知我。谢谢。

源代码

import java.util.*;

class Grids {
    private void run() {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int numRow = sc.nextInt();
        int numCol = sc.nextInt();
        int [][] rowByCol = new int [numRow][numCol];
        int [] rowFlags = new int[numRow];
        int [] colFlags = new int[numCol];

        for (int i=0;i<count;i++) {
            String cmd = sc.next();
            switch (cmd) {
                case "SIT": {
                    int r1 = sc.nextInt();
                    int c1 = sc.nextInt();
                    if (rowByCol[r1]==null) {
                        rowByCol[r1] = new int[numCol];
                    }
                    rowByCol[r1][c1] = 1;
                    rowFlags[r1] = 1;
                    colFlags[c1] = 1;
                }
                case "BOX": {
                    int r1 = sc.nextInt();
                    int c1 = sc.nextInt();
                    if (rowByCol[r1]==null || rowByCol[r1][c1]!=1) {
                        System.out.println("N");
                    } else {
                        System.out.println("Y");
                    }
                }
                case "ROW": {
                    int r1 = sc.nextInt();
                    if (rowFlags[r1]==1) {
                        System.out.println("Y");
                    } else {
                        System.out.println("N");
                    }
                }
                case "COL": {
                    int c1 = sc.nextInt();
                    if (colFlags[c1]==1) {
                        System.out.println("Y");
                    } else {
                        System.out.println("N");
                    }
                } 
                default: {
                    break;
                }
            }

        }

    }

    public static void main(String[] args) {
        Grids newGrids = new Grids();
        newGrids.run();
    }
}

输入

11 2 2
SIT 0 1
BOX 0 1
BOX 0 0
BOX 1 1
ROW 0
ROW 1
COL 0
COL 1
SIT 1 1
ROW 1
COL 1

这是我尝试使用[代码和注释中的2组输入]复制错误的简单程序。

import java.util.*;

class Main {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        String str1 = sc.next();
        String str2 = sc.next();
    }
}

//2 xyz abc

/*
2
xyz
abc
*/

0 个答案:

没有答案