gpt4 book ai didi

java - 忽略文件中的数字

转载 作者:行者123 更新时间:2023-11-30 04:39:44 25 4
gpt4 key购买 nike

我正在制作一个程序,它从文件中获取数据并从中制作迷宫游戏。 maze.txt 文件示例如下:

5  5
P.XX.
...X.
.XT..
..X..
X....

顶部的两个数字定义数组的行和列。这是我正在使用的代码

import java.util.*;
import java.io.*;

public class MazeGame {

public static void main(String[] args) throws Exception {

// Display the maze

Scanner sc = new Scanner(new File("maze.txt"));
int rows1 = sc.nextInt();
int columns1 = sc.nextInt();
rows1 += 1;
columns1 += 1;

BufferedReader in = new BufferedReader(new FileReader("maze.txt"));

char[][] treasureMaze = new char[rows1][columns1];
for (int i = 0; i < rows1 || i < columns1; ++i) {
String line = in.readLine();
if (line == null) {
System.out.println("Error in array");
return;
}
sc.nextLine();

treasureMaze[i] = line.toCharArray();
}

display(treasureMaze);
int vertical = 0;
int horizontal = 0;

// Give Move Options
options();

// Setup a while loop that continues until
// the user has gotten to the treasure, or 'P'
while (treasureMaze[vertical][horizontal] != 'T') {
// Get Users Decision
Scanner moveChoice = new Scanner(System.in);
int choice = moveChoice.nextInt();

if (choice == 1) {
System.out.println("You chose to Move up");
} else if (choice == 2) {
System.out.println("You chose to Move down");
} else if (choice == 3) {
System.out.println("You chose to Move left");
} else if (choice == 4) {
System.out.println("you chose to Move right");
} else {
return;
}

// Move the Player: Each choice will move the player
// according to their choice and then re-display the
// map and options so that they can move through the maze

// Move Up
if (choice == 1) {
if (vertical - 1 < 0) {
System.out
.println("\nCannot move there! Try something else\n");
display(treasureMaze);
options();
} else if (treasureMaze[vertical - 1][horizontal] == '.') {
treasureMaze[vertical - 1][horizontal] = 'P';
treasureMaze[vertical][horizontal] = '.';
vertical -= 1;
display(treasureMaze);
options();
} else if (treasureMaze[vertical - 1][horizontal] == 'T') {
System.out.println("\nCongratulations you won!");
treasureMaze[vertical][horizontal] = 'T';
} else {
System.out
.println("\nCannot move there! Try something else\n");
display(treasureMaze);
options();
}
}

// Move Down
else if (choice == 2) {
if (vertical + 1 < 0) {
System.out
.println("\nCannot move there! Try something else\n");
display(treasureMaze);
options();
} else if (treasureMaze[vertical + 1][horizontal] == '.') {
treasureMaze[vertical + 1][horizontal] = 'P';
treasureMaze[vertical][horizontal] = '.';
vertical += 1;
display(treasureMaze);
options();
} else if (treasureMaze[vertical + 1][horizontal] == 'T') {
System.out.println("\nCongratulations you won!");
treasureMaze[vertical][horizontal] = 'T';
} else {
System.out
.println("\nCannot move there! Try something else\n");
display(treasureMaze);
options();
}
}

// Move Left
else if (choice == 3) {
if (horizontal - 1 < 0) {
System.out
.println("\nCannot move there! Try something else\n");
display(treasureMaze);
options();
} else if (treasureMaze[vertical][horizontal - 1] == '.') {
treasureMaze[vertical][horizontal - 1] = 'P';
treasureMaze[vertical][horizontal] = '.';
horizontal -= 1;
display(treasureMaze);
options();
} else if (treasureMaze[vertical][horizontal - 1] == 'T') {
System.out.println("\nCongratulations you won!");
treasureMaze[vertical][horizontal] = 'T';
} else {
System.out
.println("\nCannot move there! Try something else\n");
display(treasureMaze);
options();
}
}

// Move Right
else if (choice == 4) {
if (horizontal + 1 < 0) {
System.out
.println("\nCannot move there! Try something else\n");
display(treasureMaze);
options();
} else if (treasureMaze[vertical][horizontal + 1] == '.') {
treasureMaze[vertical][horizontal + 1] = 'P';
treasureMaze[vertical][horizontal] = '.';
horizontal += 1;
display(treasureMaze);
options();
} else if (treasureMaze[vertical][horizontal + 1] == 'T') {
System.out.println("\nCongratulations you won!");
treasureMaze[vertical][horizontal] = 'T';
} else {
System.out
.println("\nCannot move there! Try something else\n");
display(treasureMaze);
options();
}
} else {
System.out.println("Bye!");
return;
}

}
}

// Display Object: prints out the maze for the user
public static void display(char x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[row].length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
}
}

// Options Object: gives the options menu to the user
static void options() {
System.out.println("You may:");
System.out.println("\t1) Move up");
System.out.println("\t2) Move down");
System.out.println("\t3) Move left");
System.out.println("\t4) Move right");
System.out.println("\t0) Quit");

}
}

当我尝试运行该程序时,第一次运行时我需要看到 5 5,然后将其删除,以便我可以使用程序的其余部分而不会遇到数字在上面。有没有办法忽略这些数字?

最佳答案

您可以使用 Scanner.nextLine而不是使用 BufferedReader 再次打开文件。

编辑:

为了澄清,我的意思是:

    Scanner sc = new Scanner(new File("maze.txt"));
int rows1 = sc.nextInt();
int columns1 = sc.nextInt();

char[][] treasureMaze = new char[rows1][columns1];

for (int i = 0; i < rows1; ++i) {
String line = sc.nextLine();

编辑2:

我删除了

    rows1 += 1;
columns1 += 1;

并更改了 for 循环:

我不确定向行和列添加 1 的目的是什么,循环实际上应该只循环行数 rows1。至少对于文件部分的读取来说是这样。

关于java - 忽略文件中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12591383/

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