所以,我一直在遵循本教程(https://www.youtube.com/watch?v=ugzxCcpoSdE&list=PL_QPQmz5C6WUF-pOQDsbsKbaBZqXj4qSq&index=5&ab_channel=RyiSnow),直到最后我都没有遇到任何问题,他使用 BufferedReader 读取 .txt 文件,将从 .txt 文件中提取的信息放入一个数组中,然后在数组中读取并解释为某种“映射”。 .txt 文件完全由 1 和 0 组成,向下 12 行,横向 16 行。除了一些无关紧要的名称更改之外,一切都与视频相同。问题在于代码认为 .txt 文件完全由 0 组成,并且仅输出与 0 值相关的操作。我是一个初学者,所以如果问题非常明显,请耐心等待。
我尝试使用记事本++从.txt文件中删除BOM,我已经验证了.txt文件位置,我已经删除了多余的空格、制表符,并验证了.txt文件中是否有空格。我什至向 ChatGPT 寻求帮助,但它所说的一切我已经尝试和验证过,但没有用。是的,该数组确实准确地反映了屏幕上的最大行数和列数,是的,我确实调用了 loadMap()。不过,我通过打印一些值,将可能的错误范围缩小到只有 loadMap。我发现mapTileNum全是0。绘制类很好,我通过仅打印 0 个图块和 1 个图块对其进行了测试,效果非常好。我和视频作者唯一的区别是我们的IDE不同,我用的是intellij,他用的是eclipse。任何帮助将不胜感激!这是相关类的代码(我包含了绘制类,因为我想让您知道如何输出来自 loadMap 的信息):
public void loadMap(String filepath) {
try {
InputStream is = getClass().getResourceAsStream(filepath);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int col = 0;
int row = 0;
while (col < pp.maxScreenCol && row < pp.maxScreenRow) {
String line = br.readLine();
while (col < pp.maxScreenCol) {
String[] num = line.split(" ");
int var = Integer.parseInt(num[col]);
mapTileNum[col][row] = var;
col++;
}
if (col == pp.maxScreenCol) {
col = 0;
row++;
}
}
br.close();
} catch (Exception e) {
}
}
public void draw(Graphics2D g2) {
int col = 0;
int row = 0;
int x = 0;
int y = 0;
while (col < pp.maxScreenCol && row < pp.maxScreenRow) {
int tileNum = mapTileNum[col][row];
g2.drawImage(tile[tileNum].image, x, y, pp.finalTileSize, pp.finalTileSize, null);
col++;
x += pp.finalTileSize;
if (col == pp.maxScreenCol) {
col = 0;
x = 0;
row++;
y += pp.finalTileSize;
}
}
}
}
我稍微重新安排了你的代码以消除无关的代码:
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Simple {
public int maxScreenCol = 16;
public int maxScreenRow = 12;
public int[][] mapTileNum = new int[12][16];
public void loadMap(String filepath) {
try {
InputStream is = getClass().getResourceAsStream(filepath);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
for( int row = 0; row < maxScreenRow; row++ ) {
String line = br.readLine();
String[] num = line.split(" ");
for( int col = 0; col < maxScreenCol; col++ ) {
int var = Integer.parseInt(num[col]);
mapTileNum[row][col] = var;
}
}
br.close();
} catch (Exception e) {
e.getStackTrace();
}
}
public void dumpMap()
{
for( int row = 0; row < maxScreenRow; row++ ) {
for( int col = 0; col < maxScreenCol; col++ ) {
System.out.print(mapTileNum[row][col]);
}
System.out.println();
}
}
public static void main(String[] args)
{
Simple x = new Simple();
x.loadMap("x.txt");
x.dumpMap();
}
}
如果我像这样输入“x.txt”:
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
然后程序打印:
timr@Tims-NUC:~/src$ javac x.java
timr@Tims-NUC:~/src$ java Simple
1010101010101010
0101010101010101
1010101010101010
0101010101010101
1010101010101010
0101010101010101
1010101010101010
0101010101010101
1010101010101010
0101010101010101
1010101010101010
0101010101010101
timr@Tims-NUC:~/src$
另外,请注意我交换了行和列。这是像这样的地图的传统存储方式,由一系列由列组成的行。