Computer Science Canada "quitting a java program" |
Author: | S_Grimm [ Thu Oct 02, 2008 11:30 am ] | ||
Post subject: | "quitting a java program" | ||
We are writting a code in java, and wish to know if there is a way to quit if a wrong aswer is inputed. code goes like this :
edit : the "quit" has to come in the else statement |
Author: | wtd [ Thu Oct 02, 2008 12:09 pm ] |
Post subject: | RE:"quitting a java program" |
AS it stands your program reaches the end of execution after the if/else anyway. |
Author: | S_Grimm [ Thu Oct 02, 2008 5:33 pm ] |
Post subject: | RE:"quitting a java program" |
there will be more questions after this one.... i need a statement to stick in the "else" portion of the code that will quit the program, even if there is more code. |
Author: | [Gandalf] [ Thu Oct 02, 2008 5:37 pm ] | ||
Post subject: | RE:"quitting a java program" | ||
|
Author: | wtd [ Fri Oct 03, 2008 1:57 am ] |
Post subject: | RE:"quitting a java program" |
It sounds in that case like you should be using a loop and simply exiting from it when you wish to quit. |
Author: | S_Grimm [ Fri Oct 03, 2008 10:11 am ] |
Post subject: | RE:"quitting a java program" |
I don't want a loop though.... besides if the exit loop statement is inside the if \ else statement, doesn't it just return an error? edit: Thanks Gandalf. |
Author: | wtd [ Fri Oct 03, 2008 11:28 am ] |
Post subject: | RE:"quitting a java program" |
You're doing something repetitively, so I'm pretty sure you want a loop. And no, it would not raise an exception. |
Author: | S_Grimm [ Tue Oct 07, 2008 7:03 pm ] |
Post subject: | RE:"quitting a java program" |
the loop would repeat the program. we want it to quit whenever a wrong answer is typed. we may stick it in a loop later with a "Play Again?" added, but for now, no loop. |
Author: | chili5 [ Tue Oct 07, 2008 7:37 pm ] | ||
Post subject: | RE:"quitting a java program" | ||
Use a loop, and when a wrong answer is typed just use "break" to escape the loop.
That way you can stop the program, when ever the user makes an incorrect answer. |
Author: | S_Grimm [ Wed Oct 08, 2008 7:38 am ] |
Post subject: | RE:"quitting a java program" |
i will not use break. i have found that it screws up a program royally. if anyone can prove otherwise, i invite you to now do so. Cheers. A\V |
Author: | wtd [ Wed Oct 08, 2008 1:00 pm ] |
Post subject: | RE:"quitting a java program" |
The behavior of "break" in Java is utterly predictable. I would agree with it being bad practice where unnecessary (switch), but jst because it's bad practice doesn't mean you should know how it works. Ironically you can't know that without practice. |