Help to organize game cycle in Java

Posted by ASIO22 on Game Development See other posts from Game Development or by ASIO22
Published on 2012-11-21T22:38:25Z Indexed on 2012/11/21 23:12 UTC
Read the original article Hit count: 318

Filed under:
|

I'm pretty new here (as though to a game development).
So here's my question.
I'm trying to organize a really simple game cycle in my public static main() as follows:

Scanner sc = new Scanner(System.in);
        //Running the game cycle
        boolean flag=true;
        while (flag) {
            int action;
            System.out.println("Type your action please:");
            System.out.println("0: Exit app");
            try 
            {
                action = sc.nextInt();

                switch (action) {
                    case 0:
                        flag=false;
                        break;
                    case 1:

                        break;
                }
            }   catch (InputMismatchException ex) {
                    System.out.println(ex.getClass() + "\n" + "Please type a correct input\n");
                    //action = sc.nextInt();
                    continue;
             }

What's wrong with this cycle:
I want to catch an exception when user types text instead of number, show a message, warning user, and the continue game cycle, read user input etc.

But instead of that, when users types wrong data, it goes into a eternal cycle without even prompting user.
What I did wrong?

© Game Development or respective owner

Related posts about java

Related posts about game-loop