为什么我的方法扫描文件并打印出结果比上一次打印1空间?
我目前正在编写一个很长的代码来重现康威的生活游戏,并且刚刚意识到,当我运行代码时,我通过文件扫描并将其打印出来的方法是因为矩阵无法正常工作。我已经尝试解决它,但无济于事。
public static String[][] originalBoardCreation() {
Scanner sc= MyUtils.readFile(inpFileName);
width = sc.nextInt();
height = sc.nextInt();
sc.nextLine();
String[][] board = new String[width][height];
String[] line = new String[board.length];
while (sc.hasNext()) {
for (int i = 0; i < board.length; i++) {
line[i] = sc.nextLine().trim();
for (int j = 0; j < line.length; j++) {
board[i][j] = line[j];
}
}
}
return board;
}
当我使用system.out.println(arrays.deeptoString(oinartionBoardCreation()))调用Main
[[.xxxxxxxx., null, null, null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., null, null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., .........., null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., .........., ..........]]
时x,x,x,x,x,x,x,。],...等等,但是我不知道为什么会发生这种情况或我的错误逻辑,谢谢!
I'm currently writing a quite lengthy piece of code to recreate Conway's Game of Life and have just realized that when I run my code, the method that I made to scan through a file and print itself out as matrix doesn't work as intended. I've tried messing around with it, but to no avail.
public static String[][] originalBoardCreation() {
Scanner sc= MyUtils.readFile(inpFileName);
width = sc.nextInt();
height = sc.nextInt();
sc.nextLine();
String[][] board = new String[width][height];
String[] line = new String[board.length];
while (sc.hasNext()) {
for (int i = 0; i < board.length; i++) {
line[i] = sc.nextLine().trim();
for (int j = 0; j < line.length; j++) {
board[i][j] = line[j];
}
}
}
return board;
}
When I call it to main with System.out.println(Arrays.deepToString(originalBoardCreation()));
, I get
[[.xxxxxxxx., null, null, null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., null, null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., null, null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., null, null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., null, null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., null, null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., null, null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., null, null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., .........., null], [.xxxxxxxx., x.x.x.x.x., .x.x.x.x.., xxxxxxx..., .........., .........., .........., ....xxx..., .........., ..........]]
I'm trying to get something like [[., x, x, x, x, x, x, x, x, .], ... etc
, but I have no clue as to why this is happening, so it would be great if I could get a brief explanation or error in my logic, thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信问题是由于您从文件中读取一行文本并期望将其神奇地 split splin到一个字符串数组中引起的。
我没有测试过,但是类似的事情应该起作用。
I believe the problem is being caused by the fact that your reading a line of text from a file in and expecting it to be magically split for you into a string array.
I haven't tested this, but something like this should work.