Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 "quitting a java program"
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
S_Grimm




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: 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.
S_Grimm




PostPosted: 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.
[Gandalf]




PostPosted: Thu Oct 02, 2008 5:37 pm   Post subject: RE:"quitting a java program"

code:
System.exit(0);
wtd




PostPosted: 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.
S_Grimm




PostPosted: 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.
wtd




PostPosted: 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. Smile

And no, it would not raise an exception.
S_Grimm




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
chili5




PostPosted: 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.
S_Grimm




PostPosted: 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
wtd




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: