【问题标题】:Ignoring numbers in a file忽略文件中的数字
【发布时间】:2012-09-25 21:33:10
【问题描述】:

我正在制作一个程序,它从文件中获取数据并从中制作迷宫游戏。一个示例 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,然后将其删除,这样我就可以使用程序的其余部分而不会遇到顶部的数字。有没有办法忽略这些数字?

【问题讨论】:

    标签: java arrays file io 2d


    【解决方案1】:

    您可以只使用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();
    

    EDIT2:

    我删除了

        rows1 += 1;
        columns1 += 1;
    

    并更改了 for 循环:

    我不确定将 1 添加到行和列的目的是什么,并且循环实际上应该只循环 rows1 的行数。至少对于文件部分的阅读而言。

    【讨论】:

    • 是的,我就是这么做的。我仍然收到错误消息。 “在 java.util.Scanner.nextLine(Unknown Source) 找不到任何行”是我得到的
    • rows1 += 1 表示您尝试读取的行数多于 for 循环中文件中可用的行数...
    • 我似乎仍然遇到同样的错误。你有机会知道为什么会这样吗?
    • @beny23,您缺少的是 OP 在 for 循环中有一个多余的 sc.nextLine。这是一个家庭作业,所以我不会详细说明。
    • @TimBender:呵呵,除了最初的 readLine [戴上眼镜]
    【解决方案2】:

    为什么要打开文件读取两次?

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

    使用ScannerBufferedReader,不能同时使用。

    另一种选择是在开始时简单地调用BufferedReader.readLine 以忽略第一行。

    用最少的代码改变,你实际上可以传递一个字符串来构造一个扫描器:

        //move BufferedReader creation up
        BufferedReader in = new BufferedReader(new FileReader("maze.txt"));
        Scanner sc = new Scanner(in.readLine()); //Scan first line from the Reader
    
        // The res tof your code as it exists now
        int rows1 = sc.nextInt();
        int columns1 = sc.nextInt();
        rows1 += 1;
        columns1 += 1;
    

    【讨论】:

    • 这仍然给我一个错误,说它找不到一行:“在 java.util.Scanner.nextLine(Unknown Source) 找不到行”
    • 删除那行,没用。 || i &lt; columns1 也没用。
    【解决方案3】:

    我试图创建一个类似的场景,但我的代码与你的完全不同。如果您只想忽略第 1 行上的数字,您可以在继续下一行之前简单地进行检查。 检查此代码:

    Scanner scan = new Scanner(System.in);
        System.out.println("Enter rows and columns");
        PrintWriter write = new PrintWriter("maze.txt");
        write.println(scan.next());
        write.print("a bc db gj");
        write.flush();
        write.close();
    
        BufferedReader reader = new BufferedReader(new FileReader("maze.txt"));
        String rt="";
        while((rt=reader.readLine())!=null) {
            if(rt.startsWith("5")) {
             continue;  
            }
            else {
            System.out.println(rt);
            }
        }
    
    }
    

    输入:

    5 5

    输出:a bc db gj

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多