【发布时间】:2016-12-30 19:31:25
【问题描述】:
How it should look my maze game. 这是我的代码。我的文本文件包含 0 和 1 未用空格分隔。 (33 行,43 列)。我不能将元素(逐个字符)放入矩阵中,返回的列数等于行数。
private Scanner scanner, colReader;
private int rows = 0;
private int columns = 0;
private int i = 0;
private int j = 0;
private String fileName = "C://Users//blidaru//Desktop//Lab.txt";
private String[][] matrix = null;
public Main() throws FileNotFoundException {
try {
scanner = new Scanner(new File(fileName));
}
catch (Exception e) {
System.out.println("Unable to open file");
}
while (scanner.hasNextLine()) {
++rows;
++i;
colReader = new Scanner(scanner.nextLine());
scanner.useDelimiter("");
while (colReader.hasNext()) {
++columns;
++j;
System.out.println(colReader.next() + " ");
}
}
matrix = new String[rows][columns];
System.out.println("Columns " + columns);
System.out.println("Lines " + rows);
scanner = new Scanner(new File(fileName));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < 43; ++j) {
if (scanner.hasNext()) {
matrix[i][j] = scanner.next();
}
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < 43; ++j) {
if (scanner.hasNext()) {
System.out.println(matrix[i][j]);
}
}
}
scanner.close();
colReader.close();
}
public static void main(String[] args) throws FileNotFoundException {
new Main();
}
文件Lab.txt:
1111111111111111111111111111111111111111111
1000000010001000001000000010000000100000001
1010111010101010101111101011111010111111101
1010001010100010100000001010000010000010001
1011101010111110101111111010111111111010111
.... and so on...(33 rows)
【问题讨论】:
-
不相关:
"C://Users//blidaru//Desktop//Lab.txt"应该是"C:/Users/blidaru/Desktop/Lab.txt"或"C:\\Users\\blidaru\\Desktop\\Lab.txt"。 -
那么,你想在数组中放入什么?你读的数字有多宽?只有一个字符?八字?还有什么?
-
无关: 不要把整个程序放在构造函数中。把它放在一个有名的方法中,例如
test(),并使用new Main().test()运行它。 -
嗯......这看起来像一个迷宫。是吗?
-
是的,我正在尝试制作迷宫游戏。