
-----------------------------------
S_Grimm
Thu Oct 02, 2008 11:30 am

&quot;quitting a java program&quot;
-----------------------------------
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 :

import java.io.DataInputStream;
import java.io.*;
class millionaire
{
	public static void main ( String 
edit : the "quit" has to come in the else statement

-----------------------------------
wtd
Thu Oct 02, 2008 12:09 pm

RE:&quot;quitting a java program&quot;
-----------------------------------
AS it stands your program reaches the end of execution after the if/else anyway.

-----------------------------------
S_Grimm
Thu Oct 02, 2008 5:33 pm

RE:&quot;quitting a java program&quot;
-----------------------------------
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.

-----------------------------------
[Gandalf]
Thu Oct 02, 2008 5:37 pm

RE:&quot;quitting a java program&quot;
-----------------------------------
System.exit(0);

-----------------------------------
wtd
Fri Oct 03, 2008 1:57 am

RE:&quot;quitting a java program&quot;
-----------------------------------
It sounds in that case like you should be using a loop and simply exiting from it when you wish to quit.

-----------------------------------
S_Grimm
Fri Oct 03, 2008 10:11 am

RE:&quot;quitting a java program&quot;
-----------------------------------
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.

-----------------------------------
wtd
Fri Oct 03, 2008 11:28 am

RE:&quot;quitting a java program&quot;
-----------------------------------
You're doing something repetitively, so I'm pretty sure you want a loop.  :)

And no, it would not raise an exception.

-----------------------------------
S_Grimm
Tue Oct 07, 2008 7:03 pm

RE:&quot;quitting a java program&quot;
-----------------------------------
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.

-----------------------------------
chili5
Tue Oct 07, 2008 7:37 pm

RE:&quot;quitting a java program&quot;
-----------------------------------
Use a loop, and when a wrong answer is typed just use "break" to escape the loop.


Scanner sin = new Scanner(System.in);
int n = 0;
while (true) {
System.out.println("What is 5 + 3?");
n = sin.nextInt();

if (n != 8) {
break;
}
}


That way you can stop the program, when ever the user makes an incorrect answer.

-----------------------------------
S_Grimm
Wed Oct 08, 2008 7:38 am

RE:&quot;quitting a java program&quot;
-----------------------------------
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

-----------------------------------
wtd
Wed Oct 08, 2008 1:00 pm

RE:&quot;quitting a java program&quot;
-----------------------------------
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.  :-)
