gpt4 book ai didi

java - 在Java中读取字符串中的字符时遇到问题

转载 作者:行者123 更新时间:2023-12-01 12:19:34 25 4
gpt4 key购买 nike

我正在尝试加载一个包含字符和两个数字的文本文件,如下所示

5 7
++++M++
+ + +
++ ++
+ ++ +
+X+++++

其中 5 和 7 对应于行数和列数。我试图逐行读取此内容,然后将数字转换为数组中的值,但是由于某种原因我什至无法

     Scanner inputFile = new Scanner(new FileReader(fid));
maze_r = inputFile.nextInt();
maze_c = inputFile.nextInt();
int counter = 0;
while(inputFile.hasNextLine()){

rowArray = inputFile.nextLine();
System.out.print("\n"+rowArray);


char index;
for(int ind = 0; ind == (maze_c-1);ind++){

index = rowArray.charAt(ind);
System.out.print(index);
if(index == ' ')
theMaze[counter][ind] = 0;
if(index == '+')
theMaze[counter][ind] = 1;
if(index == 'M')
theMaze[counter][ind] = 2;
if(index == 'X')
theMaze[counter][ind] = 3;

}
}
System.out.print(Arrays.toString(theMaze));
}

当我这样做时,它会将行打印为字符,但会为我尝试创建的数组打印一个空值。我不知道我做错了什么,由于某种原因它不会计算 while 循环中的 for 循环。当我在循环内写成 system.out.print 时,没有打印任何内容。我究竟做错了什么?

最佳答案

for 循环中的第二个条件是 true 才能继续循环:

ind == (maze_c-1) // ⇒ your loop breaks because ind is not equal to the rvalue

应改为:

ind < maze_c

将其视为“while”条件。

关于java - 在Java中读取字符串中的字符时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26796454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com