这是一个家庭作业问题。
这是我正在解决的迷宫的一个例子:
6 6
5 0 5 5 5 5
5 0 0 0 0 5
5 5 0 5 0 5
5 5 0 5 5 5
5 0 0 0 0 0
5 5 5 5 5 5
4 5
编辑:我的程序没有错误。我的教授希望我通过捕获ArrayIndexoutOfBoundsException
找到迷宫退出我的任务是编写程序来读取文件并将所有数字放入2D数组中。我得到了起点,并被告知编写一个程序,通过在堆栈中添加坐标来导航迷宫。 它移动到数组中的一个坐标,并以顺时针方向检查坐标,以查看是否存在潜在移动。 我让程序运行顺畅,输出正确。
然而,今天在课堂上,我的教授说我们必须在程序中捕获ArrayIndexOutOfBoundsException。
我最初刚刚创建了一个方法来检查我想要查看的坐标是否存在并且在数组中。
我的问题是,有一种简单的方法可以将异常处理实现到我已有的代码中吗?
我没有任何处理异常的经验,我在研究时看到的所有内容都只是打印出一个字符串,表示存在错误。
提前感谢您的帮助!
import java.io.File;
import java.util.*;
import java.util.Scanner;
import stackpackage.*;
public class mazeDriver {
public static void main(String[] args)
{
File inputFile = new File("maze1.txt");
Scanner in = new Scanner(inputFile);
int rows = getRows(in);
int columns = getColumns(in);
int[][]maze = createMaze(in, rows, columns);
int sRow = getSRow(in);
int sColumn = getSColumn(in);
Coordinate start = new Coordinate(sRow, sColumn);
in.close();
GoodStack stack = new GoodStack();
GoodStack answerStack = mazeRunner(start, maze, stack, columns, rows);
coursePrint(answerStack);
}
public static int getRows(Scanner in){
int rows = in.nextInt();
return rows;
}
public static int getColumns(Scanner in){
int columns = in.nextInt();
return columns;
}
public static int[][] createMaze(Scanner in, int rows, int columns){
int[][]maze = new int[rows][columns];
int rowAccumulator = 0;
int columnAccumulator = 0;
for(int i = 0; i <= (rows*columns)-1; i++){
int value = in.nextInt();
maze[rowAccumulator][columnAccumulator] = value;
if (columnAccumulator == columns - 1){
columnAccumulator = 0;
rowAccumulator += 1;
}
else{
columnAccumulator += 1;
}
}
return maze;
}
public static int getSRow(Scanner in){
int sRow = in.nextInt();
return sRow;
}
public static int getSColumn(Scanner in){
int sColumn = in.nextInt();
return sColumn;
}
/*
This method is what searches through the maze in a clockwise direction.
@param The starting point coordinates, the 2d array of the maze, the empty stack, and the maze size
@return The stack of coordinates to navigate the maze
**/
public static GoodStack mazeRunner(Coordinate startingPoint, int[][]array, GoodStack stack, int columns, int rows){
array[startingPoint.getRow()][startingPoint.getColumn()] = 1; // Sets the starting coordinate to 1
stack.push(startingPoint); // Adds the point to the stack
boolean finish = false; // Sets the value to false until the maze is finished
int skipFirst = 0; // this is a counter I used so that it does not exit on it's first run
while(finish == false){ // Create a loop to run until the maze has been finished
if(inBounds(startingPoint.getRow()-1, startingPoint.getColumn(), rows, columns)){ // checks if the array location is in the array
if (array[startingPoint.getRow()-1][startingPoint.getColumn()] == 0){ // checks if the value of that location is 0.
Coordinate location = new Coordinate(startingPoint.getRow()-1 , startingPoint.getColumn()); // creates a coordinate instance of the location
stack.push(location);// pushes the location onto the stack
array[startingPoint.getRow()-1][startingPoint.getColumn()] = 1; // sets the array value to 1 for that location
startingPoint = location; // makes the location the next search point
skipFirst +=1; // adds to the counter
continue;}} // skips the rest of the loop and moves on to the next iteration
// The next three if statements have the same effect as the last one, the only difference being it checks in the other three directions
if(inBounds(startingPoint.getRow(),startingPoint.getColumn() + 1, rows, columns)){
if(array[startingPoint.getRow()][startingPoint.getColumn()+1] == 0){
Coordinate location = new Coordinate(startingPoint.getRow(), startingPoint.getColumn()+ 1);
stack.push(location);
array[startingPoint.getRow()][startingPoint.getColumn() + 1] = 1;
startingPoint = location;
skipFirst +=1;
continue;}}
if(inBounds(startingPoint.getRow()+1, startingPoint.getColumn(), rows, columns)){
if(array[startingPoint.getRow()+1][startingPoint.getColumn()] == 0){
Coordinate location = new Coordinate(startingPoint.getRow()+1, startingPoint.getColumn());
stack.push(location);
array[startingPoint.getRow()+1][startingPoint.getColumn()] = 1;
startingPoint = location;
skipFirst +=1;
continue;}}
if(inBounds(startingPoint.getRow(),startingPoint.getColumn()-1, rows, columns)){
if(array[startingPoint.getRow()][startingPoint.getColumn()-1] == 0){
Coordinate location = new Coordinate(startingPoint.getRow(), startingPoint.getColumn()-1);
stack.push(location);
array[startingPoint.getRow()][startingPoint.getColumn()-1] = 1;
startingPoint = location;
skipFirst +=1;
continue;}}
if(mazeExit(startingPoint.getRow(), startingPoint.getColumn(), rows, columns)){ // Checks to see if there is a potential exit
if(skipFirst>2){ // This makes sure that it does not exit the maze from the entry
finish = true; // Sets the boolean to true to stop the while loop
return stack;}} // And returns the stack of coordinates
// This code happens only if it does not find any 0's to travel to
array[startingPoint.getRow()][startingPoint.getColumn()] = 1; // sets the space it is on to 1
stack.pop(); // removes the space from the stack
Coordinate nextStart = (Coordinate)stack.peek(); // takes the top coordinate off of the stack
startingPoint = nextStart; // sets the top coordinate to the starting search point
}
return stack;
}
/*
This method checks the coordinates that are about to be check to see if they are inside the array
@param The row and column that is being checked and the size of the array
@return boolean value
**/
public static boolean inBounds(int row, int column, int arrayRow, int arrayColumn){
boolean result = false;
if (row <= arrayRow -1 && row >= 0 && column <= arrayColumn-1 && column >= 0){ // checks to see if the coordinate is in the array
result = true;}
return result;
}
/*
This method is what searches through the maze in a clockwise direction.
@param The starting point coordinates, the 2d array of the maze, the empty stack, and the maze size
@return The stack of coordinates to navigate the maze
**/
public static boolean mazeExit(int row, int column, int arrayRow, int arrayColumn){
boolean result = false;
// This method checks all four directions to see if there is a potential exit
if(row == arrayRow-1){
result = true;}
if(column == arrayColumn-1){
result = true;}
if(row == 0){
result = true;}
if(column == 0){
result = true;}
return result;}
/*
This method reverses the order of the coordinates and prints it out.
@param The stack of coordinates
**/
public static void coursePrint(GoodStack stack){
List<Coordinate> list = new ArrayList<Coordinate>(); // Creates an empty arraylist
while(stack.isEmpty() == false){
Coordinate myObject = (Coordinate)stack.pop(); // takes the top coordinate from the array
list.add(myObject); // and adds it into the array list
}
Collections.reverse(list); // Reverses the order of the arraylist
for(Coordinate i:list){
System.out.println(i);} // Prints out every coordinate in the array list
}
}
答案 0 :(得分:0)
如果你真的必须结束捕获异常的程序,即使你的程序正常工作(我认为这很愚蠢,但确定),那么你可以这样做:
如果仅在位置超出范围时检查退出(mazeExit方法),那么您可以添加到指向边界之外的堆栈位置并替换
if(mazeExit(startingPoint.getRow(), startingPoint.getColumn(), rows, columns)){ // Checks to see if there is a potential exit
if(skipFirst>2){ // This makes sure that it does not exit the maze from the entry
finish = true; // Sets the boolean to true to stop the while loop
return stack;}} // And returns the stack of coordinates
// This code happens only if it does not find any 0's to travel to
array[startingPoint.getRow()][startingPoint.getColumn()] = 1; // sets the space it is on to 1
与
try{
// This code happens only if it does not find any 0's to travel to
array[startingPoint.getRow()][startingPoint.getColumn()] = 1; // sets the space it is on to 1
} catch(ArrayIndexOutOfBoundsException e) {
// Checks to see if there is a potential exit
if (skipFirst>2) { // This makes sure that it does not exit the maze from the entry
finish = true; // Sets the boolean to true to stop the while loop
return stack;
} // And returns the stack of coordinates
}
当你到达一个超出边界的位置时,这将触发Exception,因为数组[startingPoint.getRow()] [startingPoint.getColumn()]然后它将执行catch块。但要注意在你的捕获中有一个if,因为如果你的if代码不能执行,那么你需要知道你不能将数组中的值设置为1并且你不会退出该计划,请注意接下来会发生什么。
但是,如果你没有充分的理由,那么强迫捕捉异常通常不是一个好习惯。