2013-09-29 54 views
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 

CODE

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(); 
    } 

} 
+0

任何特定的原因'迷宫'是一个'字符串[] []',而不是'[char [] []']?后者更直观,会让你的生活更轻松,例如:'maze [i] = scan.nextLine()。toCharArray();'。 – Dukeling

+0

@Dukeling你应该让这个答案,我会upvote它。 –

+0

@WilliamGaul是的,但这不是一个真正的答案。通常,答案的主要部分应直接解决问题。作为补充说明,可以提到更好的做事方式。 – Dukeling

回答

0

这条线:

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

应该是:

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

的原因是,i保持不变,而你是从arrayPasser复制到maze,所以你会得到的一整行相同的字符,即(i, i)

+0

我曾经这样做,但它给了我一个超出界限例外的错误。线程“主”java.lang.ArrayIndexOutOfBoundsException中的异常:1 \t at MazeGame.main(MazeGame.java:19) – user2827416

+0

在代码中,第19行是什么? –

+0

与上面修复的arrayPasser一致的行。但修复造成了新的错误。我想我现在想知道那行代码已经修复了什么问题?数组或arrayPasser的逻辑当它试图执行这行代码时是关闭的吗?什么部分导致这个错误发生?谢谢... – user2827416

0

我其实有同样的功课问题。

但不是让2D数组成为一个字符串,我把它作为一个2D字符数组。 (因此,除了几个关键词之外没有太多变化),并且它的工作非常好......就像阅读和打印最初的“难题”一样。

// 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"); 
     } 

试试看。