Java 布尔二维数组文本文件
我需要从文本文件中读取一系列的#和空格,并将其打印为迷宫Java 布尔二维数组文本文件,java,Java,我需要从文本文件中读取一系列的#和空格,并将其打印为迷宫 public static void readMaze() throws FileNotFoundException{ String fileName = "path/maze.txt"; Scanner input = new Scanner(new File(fileName)); int x = 0; String columns = ""; while (input.hasNextLine()){ x
public static void readMaze() throws FileNotFoundException{
String fileName = "path/maze.txt";
Scanner input = new Scanner(new File(fileName));
int x = 0;
String columns = "";
while (input.hasNextLine()){
x++;
columns = input.nextLine();
}
int col = columns.length();
boolean maze[][] = new boolean[x][col];
}
我得到了迷宫的行数(x)和列数
下一步是创建一个布尔数组,文件中的每个“#”都有一个真值,每个空格都有一个假值,我不知道怎么做
while (input.hasNextLine()){
columns = input.nextLine();
for (int c = 0; c < columns.length();c++){
if (c == '#'){
add true;
}
else{
add false;
}
}
}
while(input.hasNextLine()){
columns=input.nextLine();
对于(int c=0;c
这就是下一部分,但我不确定我需要在for循环中输入什么,我们将感谢您的指导。您的难题中唯一缺少的部分是这个
int row = 0;
while (input.hasNextLine()){
columns = input.nextLine();
for (int c = 0; c < columns.length();c++){
c = columns.charAt(c);
if (c == '#'){
result[row, c] = true;
}
else{
result[row, c] = false; // not even needed, false is the default value ;)
}
row ++;
}
int行=0;
while(input.hasNextLine()){
columns=input.nextLine();
对于(int c=0;c
使用以下内容应该会更好:
ArrayList<Boolean> returnArray = new ArrayList<Boolean>()
for (int c = 0; c < columns.length();c++){
if (columns[c].equals("#")){
returnArray.add(true);
}
else{
returnArray.add(false);
}
}
return returnArray.hasArray();
ArrayList returnArray=new ArrayList()
对于(int c=0;c
inti=0;
while(input.hasNextLine())
{
row=input.nextLine();
字符串[]row_temp=row.split((?!^)”;
int row_length=row_temp.length();
对于(int j=0;jif(c=='#'){
必须是if(columns.getCharAt(c)='#'){
则需要一个行计数器,并且必须在arry-like迷宫[rowNumber][c]中输入值=trueu不必保存真/假..这将是多余的。Jens是对的。这可能会让你感兴趣:番石榴表请用解释扩展你的答案。现在它只是代码而不是答案。
int i = 0;
while(input.hasNextLine())
{
row = input.nextLine();
String[] row_temp = row.split("(?!^)");
int row_length = row_temp.length();
for(int j=0; j<row_length; j++){
if(row_temp[j].matches("#")){
maze[i][j] = true;
}
else{
maze[i][j] = false;
}
}
i++;
}