gpt4 book ai didi

java - 从文本文件读取以填充二维数组(在java中)

转载 作者:行者123 更新时间:2023-12-02 02:46:28 25 4
gpt4 key购买 nike

我有一个如下所示的文本文件:

10, 11.
(0, 0).
(3, 10).
(0, 6), (0, 7), (0, 9),
(1, 1), (1, 2), (1, 4), (1, 7),
(2, 1), (2, 7), (2, 8), (2, 10),
(3, 1), (3, 2), (3, 3), (3, 4), (3, 7),
(4, 0), (4, 6), (4, 9), (4, 10),
(5, 2), (5, 3), (5, 4), (5, 5), (5, 7),
(6, 1), (6, 8), (6, 9),
(7, 1), (7, 2), (7, 3), (7, 6),
(8, 1), (8, 5), (8, 6), (8, 8), (8, 10),
(9, 3), (9, 7).

其中第一行给出二维数组的维度,第二行应等于“S”,第三行等于“F”,所有其他行等于应为“X”的点,并且任何未列出的内容都应为“。”

我仍然需要在代码中添加更多内容,因此任何未使用的变量都是用于此目的,我只是想首先获得必须运行的内容

到目前为止我收到的错误是:

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at MazeOne.main(MazeOne.java:15)

当我尝试运行类似的代码而不读取文本文件并打印数组时,它也会给我像这样的奇怪的东西而不是数组:

  [[Ljava.lang.String;@55f96302, [Ljava.lang.String;@3d4eac69, [Ljava.lang.String;@42a57993, [Ljava.lang.String;@75b84c92]

到目前为止我的代码如下:

任何关于我出错的地方的建议或帮助将不胜感激

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
import java.util.Scanner;

public class MazeOne {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
int HEIGHT, LENGTH;
//Get file name from keyboard
File importFileName = new File("MazeOne.txt");
Scanner inFile = new Scanner("MazeOne.txt");


int Height = inFile.nextInt();
int Length = inFile.nextInt();
HEIGHT = Height-1;
LENGTH = Length-1;
String [][] MazeArray = new String[HEIGHT][LENGTH];
String [][] Start = new String[][]{{".",".","."},{".","S","."},{".",".","."}};
String [][] Block = new String[][]{{"X","X","X"},{"X","X","X"},{"X","X","X"}};
String [][] Finish = new String[][]{{".",".","."},{".","F","."},{".",".","."}};
String [][] Normal = new String[][]{{".",".","."},{".",".","."},{".",".","."}};

for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < LENGTH; j++)
{
MazeArray[i][j] = ".";
}
}

if(inFile.hasNextLine())
{
int X = inFile.nextInt();
int Y = inFile.nextInt();
MazeArray[Y][X] = "S";
}
if(inFile.hasNextLine())
{
int X = inFile.nextInt();
int Y = inFile.nextInt();
MazeArray[Y][X] = "F";
}

while(inFile.hasNextLine())
{
while(inFile.hasNextInt()){
int X = inFile.nextInt();
int Y = inFile.nextInt();
MazeArray[Y][X] = "X";
}
}
System.out.print(MazeArray);
}
}

最佳答案

在打印数组时使用此 Arrays.toString(arrayName)

关于java - 从文本文件读取以填充二维数组(在java中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44506106/

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