程序永远不会结束

时间:2014-03-07 02:54:37

标签: java eclipse

我不确定发生了什么,但是在控制台中我有一个红色的“停止”方块,我可以单击以阻止我的程序运行(Eclipse IDE)并且我的程序正在运行并且方块保持红色。 ?

编辑:

我的迷宫:

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWOOOOOOOOOOOOOOOWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

编辑:这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Stack;
import java.awt.Point;

public class MazeExplorer {
    static Point startPoint = new Point();
    static Point finishPoint = new Point();
    final static int mazeHeight = 12;
    final static int mazeWidth = 58;
    static char[][] mazePoints = new char[mazeHeight][mazeWidth];
    Stack<Point> pointsNotTraversed = new Stack<Point>();
    Point pt = new Point();
    static HashSet<Point> previousLocations = new HashSet<Point>();
    static Stack<Point> nextPoints = new Stack<Point>();

    public static void main(String[] args) throws FileNotFoundException{

        System.out.println("Please enter the file name of your Maze");
        Scanner console = new Scanner(System.in);
        File f = new File(console.nextLine());
        Scanner sc = new Scanner(f);

        if(!sc.hasNextLine()){
            System.out.println("Sorry, please enter a file name with the extension, that contains a maze!");
        }
        System.out.println("So, you want to know if your maze is solvable.....?");

        for (int row = 0; row < mazeHeight && sc.hasNext(); row++) {
            final String mazeRow = sc.next(); //Get the next row from the scanner.
            mazePoints[row] = mazeRow.toCharArray(); //Convert the row into a char[].
        }
            //identify the finish point
        for(int i = 0; i < mazeHeight; i++){
            for(int j = 0; j<mazeWidth; j++){
                if(mazePoints[i][j] == 'F'){
                    finishPoint = new Point(i, j);
                }       
            }
        }
        // Identify the start point
       for(int i = 0; i< mazeHeight; i++){
           for(int j = 0; j < mazeWidth; j++){
               if(mazePoints[i][j] == 'S'){
                 startPoint = new Point(i , j);
               }
           }
       }
       isTraversable(startPoint);    
    }
        public static  boolean isTraversable(Point current){
            boolean isSolvable = false;
            nextPoints.push(current);

            do {


                if(current.y < 11) {
                    if((mazePoints[current.y + 1][current.x] != ' ') && (mazePoints[current.y + 1][current.x] != 'W') ){ // below direction
                    nextPoints.push(new Point(current.y + 1, current.x));
                    mazePoints[current.y + 1][current.x] = ' ';        
                    isTraversable(nextPoints.pop());    

                }
                }
                if(current.y > 0){


                if (mazePoints[current.y - 1][current.x] != ' ' && mazePoints[current.y - 1][current.x] != 'W' ){ //up dir
                   nextPoints.push(new Point(current.y - 1, current.x));
                    mazePoints[current.y - 1][current.x] = ' ';  //'X' marks where you've already been
                   isTraversable(nextPoints.pop());     

                }
                }
                if(current.x < 57){
                if(mazePoints[current.y][current.x + 1] != ' ' && mazePoints[current.y][current.x + 1] != 'W'){ // to the right
                    nextPoints.push(new Point(current.y, current.x + 1));
                    mazePoints[current.y][current.x + 1] = ' ';
                    isTraversable(nextPoints.pop());    

                }
                }
                if(current.x > 0){


                if(mazePoints[current.y][current.x - 1] != ' ' && mazePoints[current.y][current.x - 1] != 'W') { // to the left
                    nextPoints.push(new Point(current.y, current.x - 1));
                    mazePoints[current.y][current.x - 1] = ' ';     
                    isTraversable(nextPoints.pop());    

                }
                }
                if(current.equals(finishPoint)){
                    isSolvable = true;
                    System.out.println("MAZE IS SOLVABLE, YAHOOOOOO!!!!");
                }




            } while(!current.equals('F') && !nextPoints.isEmpty());    


            return isSolvable;          
        }
}

4 个答案:

答案 0 :(得分:1)

你可能已经启动了多个程序并且你有“标准输出更改时显示控制台”不确定,但这解释了一个场景。如果您启动任务管理器并在那里找到该程序,您可以尝试以这种方式终止它。

答案 1 :(得分:1)

正如我之前建议的那样,您只需要重新配置递归方法。我冒昧地这样做,但如果你想学习如何编程,你会想要尝试自己解决这些问题。或者在开始编码之前尝试理解解决方案的逻辑。

你的主要问题是,在你刚刚跳入之前,你不知道你想要使用该方法的方向,这会导致各种错误,不同的东西彼此不兼容。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Stack;
import java.awt.Point;

public class TestCode {
    static Point startPoint = new Point();
    static Point finishPoint = new Point();
    final static int mazeHeight = 12;
    final static int mazeWidth = 58;
    static char[][] mazePoints = new char[mazeHeight][mazeWidth];
    Stack<Point> pointsNotTraversed = new Stack<Point>();
    Point pt = new Point();
    static HashSet<Point> previousLocations = new HashSet<Point>();
    static Stack<Point> nextPoints = new Stack<Point>();

public static void main(String[] args) throws FileNotFoundException{

    System.out.println("Please enter the file name of your Maze");
    Scanner console = new Scanner(System.in);
    File f = new File(console.nextLine());
    Scanner sc = new Scanner(f);

    if(!sc.hasNextLine()){
        System.out.println("Sorry, please enter a file name with the extension, that contains a maze!");
    }
    System.out.println("So, you want to know if your maze is solvable.....?");

    for (int row = 0; row < mazeHeight && sc.hasNext(); row++) {
        final String mazeRow = sc.next(); //Get the next row from the scanner.
        mazePoints[row] = mazeRow.toCharArray(); //Convert the row into a char[].
    }
        //identify the finish point
    for(int i = 0; i < mazeHeight; i++){
        for(int j = 0; j<mazeWidth; j++){
            if(mazePoints[i][j] == 'F'){
                finishPoint = new Point(i, j);
            }       
        }
    }
    // Identify the start point
   for(int i = 0; i< mazeHeight; i++){
       for(int j = 0; j < mazeWidth; j++){
           if(mazePoints[i][j] == 'S'){
             startPoint = new Point(i , j);
           }
       }
   }
   System.out.println(isTraversable(startPoint));    
}
    public static  boolean isTraversable(Point current){

        mazePoints[current.x][current.y] = ' ';

        if(current.y < 56 && current.y > 0 && current.x > 0 && current.x < 11){
            if (mazePoints[current.x - 1][current.y] == 'O'){ // Up dir
                Point upPoint = new Point(current.x-1, current.y);
                nextPoints.push(upPoint);
            }

            if(mazePoints[current.x+1][current.y] == 'O'){ // Down dir
                Point downPoint = new Point(current.x+1, current.y);
                nextPoints.push(downPoint);
            }

            if(mazePoints[current.x][current.y + 1] == 'O'){ // to the right
                Point rightPoint = new Point(current.x, current.y+1);
                nextPoints.push(rightPoint);
            }

            if(mazePoints[current.x][current.y - 1] == 'O'){ // to the left
                Point leftPoint = new Point(current.x, current.y-1);
                nextPoints.push(leftPoint);
            }

            if(mazePoints[current.x - 1][current.y] == 'F' ||
               mazePoints[current.x + 1][current.y] == 'F' ||
               mazePoints[current.x][current.y - 1] == 'F' ||
               mazePoints[current.x][current.y + 1] == 'F'){
                System.out.println("MAZE IS SOLVABLE, YAHOOOOOO!!!!");
                return true;
            }

        }
        if(nextPoints.isEmpty()){
            return false;
        }
        else{
            current = nextPoints.pop();
        }

        return(isTraversable(current));

    }
}

使用迷宫输入:

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWOOOOOOOOOOOOOOOWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWOFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

产生以下输出:

So, you want to know if your maze is solvable.....?
MAZE IS SOLVABLE, YAHOOOOOO!!!!
true

我以不同的方式导入文件,但您可以将其更改回您之前使用的任何方法。

答案 2 :(得分:0)

如果它停止运行并且永远不会完成,您可以尝试在Eclipse调试器中运行程序,而不会有任何断点。

打开Debug选项卡(从Window&gt; Show View&gt; Debug中打开),右键单击'Thread [main](Running)'并选择'Suspend'来暂停该线程。

然后从堆栈的底部向上工作,希望这会缩小到足以让你找到阻塞的地方。

答案 3 :(得分:0)

import java.util.Scanner;
import java.util.Stack;


public class test5 {

private int numRows;
private int numCols;

public test5(){
 Scanner input = new Scanner(System.in);
 System.out.println("Enter number of rows: ");
 numRows = input.nextInt();
 System.out.println("Enter number of cols: ");
 numCols = input.nextInt();

 Stack<Point>stack = new Stack<Point>();
 stack.push(new Point(0,0));


 while(!stack.isEmpty()){

     if(currentPath(stack.pop(),stack)){
         System.out.println("Maze is solvable");
     }



 }

}

public static void main(String[]args){

    new test5();

}

private boolean currentPath(Point point, Stack<Point>stack){
    int currentRow = point.getRow();
    int currentCol = point.getCol();

    while(currentRow!=numRows-1 || currentCol!=numCols-1){
        boolean canGoRight = canGoRight(currentRow,currentCol);
        boolean canGoUp = canGoUp(currentRow,currentCol);
        boolean canGoDown = canGoDown(currentRow,currentCol);

        if(canGoRight){
            if(canGoUp){
                stack.push(new Point(currentRow-1,currentCol));
            }
            if(canGoDown){
                stack.push(new Point(currentRow+1,currentCol));
            }
            currentCol = currentCol+1;
        }
        else{
            if(canGoUp){
                if(canGoDown){
                    stack.push(new Point(currentRow+1,currentCol));

                }
                currentRow = currentRow-1;

            }
            else if(canGoDown){
                currentRow = currentRow+1;
            }
            else{
                return false;
            }

        }
    }

    return true;
}



private  boolean canGoUp(int row, int col){
    return row-1>=0; 
}

private  boolean canGoRight(int row, int col){
    return col+1<numCols;
}

private  boolean canGoDown(int row, int col){
    return row+1<numRows;
}

class Point{
    private int row;
    private int col;

    public Point(int row, int col){
        this.row = row;
        this.col = col;
    }

    public int getRow(){
        return row;
    }

    public int getCol(){
        return col;
    }
}
}