0

迷宫和导航的打印输出有错误,我相信这是我从文件中扫描的时候,但我可能是错的。

文件输入

5 5
P.XX.
.X...
...X.
XXT..
..X..

我看到的样本

P P P P P 
X X X X X 
. . . . . 
. . . . . 

You may:
1) Move up
2) Move down
3) Move left
4) Move right
0) Quit

我应该看到的

P.XX.
.X...
...X.
XXT..
..X..

You may:
1) Move up
2) Move down
3) Move left
4) Move right
0) Quit

代码

import java.util.*;
import java.io.File;
public class MazeGame {

public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new File("maze.txt"));
Scanner user = new Scanner(System.in);
int rows = scan.nextInt();
int columns = scan.nextInt();
int px = 0;
int py = 0;
String [][] maze = new String[rows][columns];
//String junk = scan.nextLine();

for (int i = 0; i < rows; i++){
    String temp = scan.nextLine();
    String[] arrayPasser = temp.split("");
    for (int j = 0; j < columns; j++){
        maze[i][j] = arrayPasser[i];
    }
}

boolean gotTreasure = false;

while (gotTreasure == false){
    for (int i = 0; i < rows; i++){
        for (int j = 0; j < columns; j++){
            System.out.print(maze[i][j]);
            System.out.print(" ");
    }
        System.out.print("\n");
  }


    System.out.printf("\n");
    System.out.println("You may:");
    System.out.println("1) Move up");
    System.out.println("2) Move down");
    System.out.println("3) Move left");
    System.out.println("4) Move right");
    System.out.println("0) Quit");
    int choice = user.nextInt();
    int i = 0;

    if (choice == 1 && i >= 0 && i < columns){
        for (int k = 0; k < rows; k++){
            for (int l = 0; l < columns; l++){
                if (maze[k][l].equals(maze[px][py]) && maze[px][py-1].equals("X") == false){
                    maze[px][py] = ".";
                    maze[k][l-1] = "P";
                    maze[px][py] = maze[k][l-1];
                }else if (maze[px][py-1] == "X"){
                    System.out.println("Cannot move into a cave-in! Try something else.");
                }else {
                continue;}


                }
            }
        }
    else if (choice == 2 && i >= 0 && i < columns){
        for (int k = 0; k < rows; k++){
            for (int l = 0; l < columns; l++){
                if (maze[k][l].equals(maze[px][py]) && maze[px][py+1].equals("X") == false){
                    maze[px][py] = ".";
                    maze[k][l+1] = "P";
                    maze[px][py] = maze[k][l+1];
                }else if (maze[px][py+1] == "X"){
                    System.out.println("Cannot move into a cave-in! Try something else.");
                }else {
                continue;}

           }
         }
        }
    else if (choice == 3 && i >= 0 && i < columns){
        for (int k = 0; k < rows; k++){
            for (int l = 0; l < columns; l++){
                if (maze[k][l].equals(maze[px][py]) && maze[px-1][py].equals("X") == false){
                    maze[px][py] = ".";
                    maze[k-1][l] = "P";
                    maze[px][py] = maze[k-1][l];
                }else if (maze[px-1][py] == "X"){
                    System.out.println("Cannot move into a cave-in! Try something else.");
                }else {
                continue;}
            }
        }
    }
    else if (choice == 4 && i >= 0 && i < columns){
        for (int k = 0; k < rows; k++){
            for (int l = 0; l < columns; l++){
                if (maze[k][l].equals(maze[px][py]) && maze[px+1][py].equals("X") == false){
                    maze[px][py] = ".";
                    maze[k+1][l] = "P";
                    maze[px][py] = maze[k+1][l];
                }else if (maze[px+1][py] == "X"){
                    System.out.println("Cannot move into a cave-in! Try something else.");
                }else {
                continue;}
            }
        }
    }
    else if (choice == 0){
        System.exit(0);
    }
}

System.out.println("Congratulations, you found the treasure!");

scan.close();
user.close();
    }

}
4

2 回答 2

0

这一行:

maze[i][j] = arrayPasser[i];

应该:

maze[i][j] = arrayPasser[j];

原因是,当您从toi复制时保持不变,因此您将获得相同字符的一整行,即 at 的那个。arrayPassermaze(i, i)

于 2013-09-29T01:09:47.327 回答
0

我实际上有同样的家庭作业问题。

但我没有将二维数组作为字符串,而是将其作为二维字符数组。(因此除了几个关键词之外没有太大变化)而且它工作得非常好……就阅读和打印最初的“拼图”而言。

// Create maze in 2 dimensional character array.
    char[][] maze = new char[R][C];
    input1.nextLine();

    // Scan maze.
    for (int i = 0; i < R; i++) {
        String row = input1.next();
        for (int j = 0; j < C; j++)
            maze[i][j] = row.charAt(j);
    }
// Print Maze. (in your while loop)
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                System.out.print(maze[i][j]);
                System.out.print(" ");
            }
            System.out.print("\n");
        }

试试看。

于 2013-10-03T00:10:31.920 回答