从文本到二维数组的网格
import java.io.*;
import java.util.*;
import java.awt.*;
public class FileInputExample2
{
static public void main(String[] args) throws IOException
{
int t;
BufferedReader filein;
filein = new BufferedReader (new FileReader("GridDATA.txt"));
int intGrid [] [] = new int [10] [10];
String inputLine = filein.readLine();
StringTokenizer st = new StringTokenizer(inputLine, " ");
for (int i=0; i<10; i++)
for (int j=0; j<10; j++)
{String eachNumber = st.nextToken();
intGrid [i] [j] = Integer.parseInt(eachNumber);
}
for (int i=0; i<10; i++)
for (int j=0; j<10; j++)
{
System.out.println( intGrid[i][j]);
}
}
}
这就是我到目前为止所拥有的,我试图显示这个网格,我的文本文件如下所示:
0 1 1 1 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1 1
1 1 0 1 1 1 1 0 0 0
1 1 0 0 0 0 1 0 1 0
1 1 1 1 1 0 1 0 1 0
1 1 1 1 1 0 0 0 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
我不知道为什么它不工作。最终我会建造一个迷宫。
import java.io.*;
import java.util.*;
import java.awt.*;
public class FileInputExample2
{
static public void main(String[] args) throws IOException
{
int t;
BufferedReader filein;
filein = new BufferedReader (new FileReader("GridDATA.txt"));
int intGrid [] [] = new int [10] [10];
String inputLine = filein.readLine();
StringTokenizer st = new StringTokenizer(inputLine, " ");
for (int i=0; i<10; i++)
for (int j=0; j<10; j++)
{String eachNumber = st.nextToken();
intGrid [i] [j] = Integer.parseInt(eachNumber);
}
for (int i=0; i<10; i++)
for (int j=0; j<10; j++)
{
System.out.println( intGrid[i][j]);
}
}
}
this is what i have so far im trying to display this grid that i have the text file looks like this :
0 1 1 1 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1 1
1 1 0 1 1 1 1 0 0 0
1 1 0 0 0 0 1 0 1 0
1 1 1 1 1 0 1 0 1 0
1 1 1 1 1 0 0 0 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
I have no idea why its not wokring. ultimatly i wil be makin a maze.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一方面,
System.out.println(intGrid[i][j]);
将每行打印一个网格元素。您可能想要更多类似的内容,
请注意,我们在内循环中使用
print
,而不是 println。这不会执行回车,因此数字将在一行上。然而,在内循环之后,我们执行 println 来执行回车/换行。For one thing,
System.out.println( intGrid[i][j]);
will print one grid element per line.You probably want something more like
Note that we're using
print
in the inner loop, not println. This will not perform a carriage return so the numbers will be on one line. After the inner loop, however, we execute aprintln
to perform the carriage return/newline.