|
Author |
Topic: Switch / Case problem (Read 324 times) |
|
spectre_240sx
|
Switch / Case problem
« on: Apr 15th, 2004, 3:37am » |
|
Ok, how can anyone have a problem with this, right? Well, I figured out a way. Anyway, I've got a pretty simple statement, but for some reason, the values keep going to default rather than one of the cases. Here's the code. It's in a method of a class called ant. Direction is defined as an int in the begining of the class and called in the constructor. Code: switch (Direction) { case 0: posY++; case 1: posX++; posY++; case 2: posX++; case 3: posX++; posY--; case 4: posY--; case 5: posX--; posY--; case 6: posX--; case 7: posX--; posY++; default: println("Error. " + Direction); } |
| at least 50% of the time I get a return of "error..." but the direction listed is one with a specific case already assigned... anyone have any ideas of what might be the problem?
|
|
|
|
mungbean
|
Re: Switch / Case problem
« Reply #1 on: Apr 15th, 2004, 4:02am » |
|
you need a "break" (don't we all?) after each case. Code: and so on. Otherwise execution will carry on into the next case (which can sometimes be useful, but not often.)
|
|
|
|
|