"quitting a java program"
Author |
Message |
S_Grimm
![](http://compsci.ca/v3/uploads/user_avatars/18926424384e6d86e5cf4d6.jpg)
|
Posted: 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 :
Java: |
import java.io.DataInputStream;
import java.io.*;
class millionaire
{
public static void main ( String [] args ) throws IOException
{
//Works as Who wants to be a Millionaire. If you answer correct you will gain money whilst if you answer incorrectly you will lose money.
BufferedReader input = new BufferedReader ( new InputStreamReader ( System. in ) );
String answer;
System. out. println( "PLEASE ANSWER ALL QUESTIONS BY INPUTTING LETTERS 'A' - 'D' ON YOUR KEYPAD!" );
System. out. println( "Which of the following is no longer an NHL Hockey team?" );
System. out. println( "(a) Minnesota Wild" );
System. out. println( "(b) Quebec Nordiques" );
System. out. println( "(c) Nashville Predators" );
System. out. println( "(d) Edmonton Oilers\n" );
answer = input. readLine();
System. out. println( " " );
if ( answer == "b" ); {
System. out. println( "Congratulations! You have $100.00" );
System. out. println( "$100 - Completed\n$200\n$400\n$1,000\n$2,000\n$8,000\n$25,000" );
System. out. println( "$50,000\n$150,000\n$500,000\n$1,000,000" );
}
else {
System. out. println ( "I'm Sorry, that answer is incorrect. Your total winnings are $0" );
System. out. println ( "$100 - Epic Fail\n$200\n$400\n$1,000\n$2,000\n$8,000\n$25,000" );
System. out. println( "$50,000\n$150,000\n$500,000\n$1,000,000" );
}
System. out. println ( "
}
}
|
edit : the "quit" has to come in the else statement |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
S_Grimm
![](http://compsci.ca/v3/uploads/user_avatars/18926424384e6d86e5cf4d6.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Thu Oct 02, 2008 5:37 pm Post subject: RE:"quitting a java program" |
|
|
|
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
S_Grimm
![](http://compsci.ca/v3/uploads/user_avatars/18926424384e6d86e5cf4d6.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
S_Grimm
![](http://compsci.ca/v3/uploads/user_avatars/18926424384e6d86e5cf4d6.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
chili5
![](http://compsci.ca/v3/uploads/user_avatars/218321676502865ca3e379.jpg)
|
Posted: 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.
code: |
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. |
|
|
|
|
![](images/spacer.gif) |
S_Grimm
![](http://compsci.ca/v3/uploads/user_avatars/18926424384e6d86e5cf4d6.jpg)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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. ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|