【发布时间】:2019-11-16 12:32:01
【问题描述】:
这里是Java新手,尝试扫描以下表单中的输入:
3
3 3
101
000
101
1 2
11
5 5
10001
00000
00000
00000
10001
(第一个数字是测试用例的数量,然后是行和列,0s和1s是世界的状态,等等。基本上和任何典型的BFS问题一样)
下面是我尝试从控制台获取输入的 Java 代码的一部分。 cmets 是我在调试时在 watch 部分看到的值:
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = in.nextInt(); // t: 3, in.nextInt(): 3 as expected
for (int i = 1; i <= t; i++) { // i: 1, t: 3, in.nextInt(): 3 (not sure which "3")
int row = in.nextInt();
// after this line in.nextInt(): 101 and row: 0 which are all WEIRD
int column = in.nextInt();
// now here it's in.nextInt(): 2 and column: 2
int world[][] = new int[row][column];
// in.nextInt(): 11 now and as we see it changed its value when I didn't even call it
TreeMap<Integer, ArrayList<int[]>> distancesRandAcc = new TreeMap<Integer, ArrayList<int[]>>();
distancesRandAcc.put(0, new ArrayList<int[]>());
for (int j = 0; j < row; j++) {
int temp = in.nextInt();
char[] s = Integer.toString(temp).toCharArray();
for (int k = 0; k < column; k++){
world[j][k] = Character.getNumericValue(s[k]);
}
}
int result = calculateDeliveryTime(world, row, column, distancesRandAcc);
System.out.println("Case #" + i + ": " + result);
}
}
这做了一些超出“常识”的奇怪事情。在将所有输入放入 IntelliJ 控制台后,我正在逐行调试,似乎 in.nextInt() 无法停止在随机行中获取随机数量的令牌无论我是否调用它。
当我在逐行调试时尝试逐行输入输入时,似乎对 .nextInt 的一次调用要求两个 int 值。 (我输入了一个数字并输入,但它仍然在等待另一个输入,同时停留在 .nextInt() 只调用一次的同一行)
此时,我感觉我的调试过程是错误的,或者至少是一些外部问题,而不是代码本身。我可能错了,因为我仍然认为自己是新手......
请有经验的人教我如何解决这个问题?我发现一些问题与 .nextLine() 和 .nextInt() 有类似的问题,但不完全是这样。如果它真的是重复的,我提前道歉......
(如果需要我会添加更多信息)
【问题讨论】: