-2

下面显示的代码是我正在创建的迷宫游戏的主要方法。在这个主要方法中,我有一个 while 循环,一直持续到游戏结束。在 while 循环中,我打印迷宫,然后询问用户他们想要移动角色的方向。我有一个疯狂的问题,提示用户询问密钥,不会显示。输出只是打印迷宫,然后等待用户输入。

        do
        {               
            maze.printArray();              
            //Asks the user which direction they wish to move THIS LINE NEVER SHOWS UP!
            System.out.print ("Enter U,L,D,R to indicate move direction:   ");              
            //Gets the string from user, and changes it to a char, then converts it to an uppercase 
            direction = read.next().charAt(0);
            direction = Character.toUpperCase(direction);                   
            //Moves the character and increments the invalidMoves counter if the move was not legal
            if(maze.move(direction) == false)
                invalidMoves++;                 
            //Increment the move count regardless of its validity
            moves++;
        }
        while(maze.isFinished() == false);

这是在打印之前调用的 printArray 方法。

public void printArray() 
{
    for (char [] x : mazeArray) 
    {
        for (char y : x) 
        {
            //Print the characters for the row
            System.out.print (y);
        }           
        //linebreak to go to next row
        System.out.println ();
    }
    System.out.println ();
}

2D 数组包含一个迷宫图案,它会显示在屏幕上

XXXXXXXXXX
XO-------X
XX-XXX-XXX
XX---X---X
XXXX-X-X-X
X----X-XXX
X-XXXX--$X
XXXXXXXXXX
4

3 回答 3

1

System.out.flush();之后试试print()

于 2012-05-01T00:42:01.233 回答
1

如果您的代码调用了 System.setOut(printStream),您可以使用以下命令将其恢复为标准输出:

System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));

如果在调用进程时 stdout 被重定向到某个地方,您可以使用以下命令指定您自己的 PrintStream:

System.setOut(new PrintStream(new FileOutputStream("out.txt")));

或者您可以直接写入 PrintStream:

PrintStream ps = new PrintStream(new FileOutputStream("out.txt"));
ps.println("hi");
于 2012-05-01T05:49:46.890 回答
0

尝试println而不是print...

于 2012-05-01T00:38:04.317 回答