samedi 28 février 2015

Error using simple IF statements, bolean variables and char marices

I'm writing a program that indicates if a maze, which is a matrix of chars, has a path. My first step is to verify that the first & last rows contain a specific char. the code is very simple yet it turns to work the opposite way. This is the code:


public static void main(String[] args) { // TODO Auto-generated method stub



final int ROWS = 5;
final int COLUMNS = 6;
int entryIndex = -1, exitIndex = -1, row;
boolean work = true;
boolean entryFound = false;
boolean exitFound = false;
char maze[][] = { {'a', '|', 'a', 'a', 'a', '|'},
{'a', 'a', 'a', 'a', 'a', '|'},
{'a', 'a', 'a', 'a', 'a', '|'},
{'a', 'a', 'a', 'a', 'a', '|'},
{'a', 'a', 'a', 'a', 'a', '|'} };

for (int col=0; col < COLUMNS; col++) {
if (maze[0][col] == '|') {
entryFound = true;
entryIndex = col;
}

for (int col2=0; col2 < COLUMNS; col2++) {
if (maze[ROWS-1][col2] == '|') {
exitFound = true;
exitIndex = col2;
}

if (entryFound == false || exitFound == false) {
//work = false;
System.out.println("No entry or exit for the maze had been found. Quitting");
return;
}


What I get at the end is the message that says "No entry or Exit had been found". How could this be? What could go wrong here?


Aucun commentaire:

Enregistrer un commentaire