package maze;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Stack;class Maze{
    int rows = 0, cols = 0;
    char[][] store;
    MazeCell currentCell, exitCell = new MazeCell(),
            entryCell = new MazeCell();
    final char exitMarker = 'e', entryMarker = 'm', visited = '.';
    final char passage = '0', wall = '1';
    Stack mazeStack = new Stack();    public Maze(){
        int row = 0, col = 0;
        Stack mazeRows = new Stack();
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader buffer = new BufferedReader(isr);
        String str;
        System.out.println("Enter a rectangular maze using the following "
                + "characters:\nm - enter\ne - exit\n1 - wall\n0 - passage\n"
                + "Enter one line at time; end with Ctrl-Z");
        try{
            str = buffer.readLine();
//??????????????????????????????????????????
            while(str != null){//?????死循环,怎么也跳不出???????????????
//??????????????????????????????????????????
                row++;//行++
                cols = str.length();
                str = "1" + str + "1";//前后填补"1"
                mazeRows.push(str);//串进栈
              //System.out.println(mazeRows);
                if(str.indexOf(exitMarker) != -1){//有出口
                    entryCell.x = row;
                    entryCell.y = str.indexOf(exitMarker);
                }//System.out.println(entryCell);
                if(str.indexOf(entryMarker) != -1){//有入口
                    exitCell.x = row;
                    exitCell.y = str.indexOf(entryMarker);
                }//System.out.println(entryMarker);
               str = buffer.readLine();
            }
        }
        catch(IOException eof){
        }
        rows = row;
        store = new char[rows + 2][];System.out.println(store);
        store[0] = new char[cols + 2];System.out.println(store[0]);
        for(; !mazeRows.isEmpty(); row--)//mazeRows不为空
            store[row] = ((String) mazeRows.pop()).toCharArray();System.out.println(store[row]);
        store[rows + 1] = new char[cols + 2];
        for(col = 0; col <= cols + 1; col++){//左右补墙
            store[0][col] = wall;
            store[rows + 1][col] = wall;
        }
        // TODO Auto-generated constructor stub
    }    void display(PrintStream out){
        for(int row = 0; row <= rows + 1; row++)
            out.println(store[row]);//打印行
        out.println();//换行
    }    void pushUnvisited(int row,int col){
        if(store[row][col] == passage || store[row][col] == exitMarker) 
            mazeStack.push(new MazeCell(row,col));
    }    void exitMaze(PrintStream out){
        int row = 0, col = 0;
        currentCell = entryCell;
        out.println();//换行
        while(!currentCell.equals(exitCell)){//当前不为出口
            row = currentCell.x;
            col = currentCell.y;
            display(System.out);//打印行
            if(!currentCell.equals(entryCell)) store[row][col] = visited;//当前不为入口就打"."
            pushUnvisited(row - 1,col);//向上
            pushUnvisited(row + 1,col);//向下
            pushUnvisited(row,col - 1);//向左
            pushUnvisited(row,col + 1);//向右
            if(mazeStack.isEmpty()){//mazeStack为空
                display(out);
                out.println("Failure");
                return;
            }
            else currentCell = (MazeCell) mazeStack.pop();//出栈
        }
        display(out);
        out.println("Success");
    }    public static void main(String[] args){
        (new Maze()).exitMaze(System.out);
    }
}
//----------------------------------------------------------------------------------------
/*
 * Created on 2005-4-29
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package maze;
/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
class MazeCell{
    int x, y;    MazeCell(){
    }    MazeCell(int i,int j){
        x = i;
        y = j;
    }    boolean equals(MazeCell cell){
        return x == cell.x && y == cell.y;
    }
}

解决方案 »

  1.   

    String str;
                
    str = buffer.readLine();
    while(str != null)只要不为空当然循环要进行下去了
      

  2.   

    应该是
    while( (str = buffer.readLine()) != null){
    ...
      

  3.   

    上面两位的方法我都试过。
    我什么都不输入就按回车就应该是输入空串了吧??这样不行。while( (str = buffer.readLine()) != null){也不行。你们把代码复制进去试试。
      

  4.   

    while((str != null)&&(!str.trim().equals("")))
      

  5.   

    根据提示“e -exit”应判断:
    while(!str.eaquals("e")){//?????死循环,怎么也跳不
      

  6.   

    你自己的程序里不都已经打印出来了么System.out.println("Enter a rectangular maze using the following "
    + "characters:\nm - enter\ne - exit\n1 - wall\n0 - passage\n"
    + "Enter one line at time; end with Ctrl-Z");用Ctrl-Z来结束输入,这个时候buffer.readLine()会返回 null
      

  7.   

    用Ctrl-Z来结束输入
    是输入  Ctrl-Z  然后回车马?不行
    这是书上的程序。
      

  8.   

    用Ctrl-Z来结束输入
    是输入  Ctrl-Z  然后回车马?不行
    这是书上的程序。
      

  9.   

    就是同时按Ctrl键和Z键然后回车,不是输入Ctrl-Z这几个字母!!!