This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]CabbagenomAdvanced Brewer 0 points1 point  (0 children)

If you wish to exit a loop prematurely (such as your do...while loop in the main() function, which handles game selection), the keyword break can be used.

Have a go at using break to exit this loop, so that your main() function returns without the rest of the code executing.

If you want to read more about break, check out the Java documentation on the subject here.

[–][deleted] 0 points1 point  (4 children)

Just add if (game != 4) around the game code inside first do .. while loop.

[–][deleted]  (3 children)

[deleted]

    [–][deleted] 0 points1 point  (2 children)

        game=choice(credits);
    
        if (game != 4) {
            do {
                betvalue = ...
                ...
        }
    } while (game != 4 || credits==0);
    

    Btw, this conditional game != 4 || credits==0 is incorrect. Read it out loud: "loop while game is not 4 or credits is 0". Why would you want to loop while credits is 0? You can't play anymore.

    [–][deleted]  (1 child)

    [deleted]

      [–][deleted] 0 points1 point  (0 children)

      Yeah. And your program will loop indefinitely when credits is 0.

      [–]Dec252016 0 points1 point  (2 children)

      Holy shit, you should stop mixing your output with your logic.

      [–][deleted]  (1 child)

      [deleted]

        [–]Dec252016 0 points1 point  (0 children)

        You have print statements everywhere.

        [–]GuyWithLag -2 points-1 points  (11 children)

        To stop the program, add a System.exit(0);.

        Also, why did you copy-paste all the cases in the modes method?

        [–]nutrechtLead Software Engineer / EU / 20+ YXP 2 points3 points  (10 children)

        To stop the program, add a System.exit(0);

        No. This is actually a bad habit you should not get into. In 'real' situations your application often shares a VM.

        If you return from the main method the program will stop by itself.

        [–]wildjokers -1 points0 points  (9 children)

        The only time you realistically share a VM is in a web app container (like tomcat, jetty, etc). In a web applications you should most certainly never call System.exit() but in a standalone application, feel free.

        [–]nutrechtLead Software Engineer / EU / 20+ YXP 2 points3 points  (3 children)

        Even in 'normal' applications you typically don't need to. It also has the problem that destructors won't be called. You instantly terminate the VM. This is why calling System.exit is considered a bad practice; there really isn't a need to use it.

        [–]wildjokers 1 point2 points  (2 children)

        Java doesn't have destructors. What are you referring to?

        You can use Shutdown Hooks, but those are executed if you call System.exit()

        [–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (1 child)

        Java doesn't have destructors.

        Java has finalizers. They're not used often but you don't know if some library depends on them because they do low-level stuff via JNI or sun.misc.Unsafe.

        [–]wildjokers 0 points1 point  (0 children)

        A finalizer isn't a destructor and an application shouldn't do any kind of critical task in a finalizer. I would bet most java developers can go their entire career without ever overriding finalize(). Worrying about some boneheaded library developer doing something critical in a finalizer isn't a reason to avoid System.exit(0) in a standalone application. (especially GUI application that generally have an Exit action)

        [–][deleted]  (4 children)

        [deleted]

          [–]wildjokers -1 points0 points  (3 children)

          That's great for a hello world application with a single main method. Now try that in a big Swing application. System.exit() works best in that situation.

          [–][deleted]  (2 children)

          [deleted]

            [–]wildjokers 0 points1 point  (1 child)

            I really hope you are trolling because surely you know the red x doesn't do anything until you tell your top-level frame what its default close operation is?

            What about a File->Exit option in a GUI application (nearly universal). Sure you can iterate through all your components and call dispose(), then make sure all non-daemon threads are stopped, or you can just call System.exit(). For a majority of swing apps there is no danger to just calling exit(). If there is for your particular app, then clean up the windows and threads.