2014-03-07 53 views
-1

我不确定发生了什么事,但在控制台中我有一个红色的“停止”方块,我可以单击以停止运行(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;   
     } 
} 
+0

非常,非常模糊。如果您认为这是因为您的代码,请将您的代码发布到此处。如果它仍然存在任何代码,那么最好到超级用户这里,而不是SO。 – Manhattan

+0

那么,你有无限循环?你的计划是什么? – Kyranstar

+0

你已经粘贴了一段随机代码,并声明你的程序不会退出。这是不够的信息。 –

回答

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 

我导入的文件不同的方式,但你可以改变这回你使用任何方法先前。

+0

这应该在传入起始点的主方法中调用? – bazookyelmo

+0

是的。我这样做,在打印语句'的System.out.println(isTraversable(的startPoint));' – leigero

+0

,将打印“真”,以及在你的方法写的“yahoooo”行。 – leigero

1

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

+0

试过,仍然无限... – bazookyelmo

+1

道歉,但猜测不答案。相反,这更适合作为评论。 – Manhattan

+1

如果我有足够的代表,我会做出这个评论:)一个长镜头,但它可能是答案! –

0

如果它卡住运行,并且从未完成,您可以尝试在Eclipse调试器中运行您的程序,而没有任何断点。

打开调试选项卡(从窗口打开>显示视图>调试),右键单击“线程[主要](正在运行)”并选择“挂起”挂起线程。

然后从堆栈底部您的方式工作,希望这将缩小它足以让你找到它的块。

+0

已经尝试过,没有效果 – bazookyelmo

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; 
    } 
} 
} 
+0

可能是一个愚蠢的问题......但我的迷宫文件去哪里? – bazookyelmo

+0

如果你使用eclipse,你可以把它放在项目文件夹中。 – Solace