【发布时间】:2018-03-31 01:31:48
【问题描述】:
这是我要解决的问题的链接:https://dmoj.ca/problem/dmopc14c1p5
这是我为接收输入而编写的代码。
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
int y = kbd.nextInt(); //number of rows
int x = kbd.nextInt(); //number of columns
int initX = kbd.nextInt(); //initial starting position
int initY = kbd.nextInt();
int endX = kbd.nextInt(); //ending position (main office)
int endY = kbd.nextInt();
char [] [] maze = new char [y][x];
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
maze[i][j] = kbd.next().charAt(0);
}
}
//For checking
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
System.out.print(maze[i][j]);
}
System.out.println();
}
}
但是,我不知道如何在 for 循环中正确接收字符输入。我使用了通过此链接 (How to take input for 'char' array in Java?) 找到的scanner.next().charAt(0) 方法,但它会导致一个无限循环,无论我输入多少字符都不会结束。
我做错了什么?
更新:
这是我将收到的输入(字符之间没有空格):
OOXOO
OXOXO
OOOXX
XOXOX
OOOOO
我应该如何修改我的代码以使读取此输入成为可能?
【问题讨论】: