lundi 24 octobre 2016

wrong value in while statement java

Fairly new to coding but I am struggling to find the issue in my code. It is just a class which will run a few other classes and ask if you want to play again or not.

The issue is when you choose the first game and then say n or no to not play again when you play a second game after answering that you would like to play again it goes back to asking what game you would play instead of repeating the game.

public static void arcade() {
    Scanner scanner2 = new Scanner(System.in);
    String choice;
    int game;
    boolean finish;
    finish = false;
    game = 0;

    do {
        try {

            System.out.println(
                    "Which game would you like to play? \n1. Coin toss\n2. Rock Paper Scissors \n3. Number Game\n4. Exit");
            game = scanner2.nextInt();

            switch (game) {
            case 1:
                choice = "yes";
                do {
                    if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) {
                        coinToss(scanner2);
                        System.out.println("Would you like to play again?");
                        choice = scanner2.next();
                    } else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) {
                        System.out.println("Goodbye");
                        finish = true;
                    } else {
                        System.out.println("Invalid selection");
                        choice = scanner2.next();

                    }
                } while (finish != true);
                break;
            case 2:
                choice = "yes";
                do {
                    if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) {
                        rockPaperScissors(scanner2);
                        System.out.println("Would you like to play again?");
                        choice = scanner2.next();
                    } else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) {
                        System.out.println("Goodbye");
                        finish = true;

                    } else {
                        System.out.println("Invalid selection");

                    }
                } while (finish != true);
                break;
            case 3:
                choice = "yes";
                do {
                    if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("y")) {
                        numberGame(scanner2);
                        System.out.println("Would you like to play again?");
                    } else if (choice.equalsIgnoreCase("no") || choice.equalsIgnoreCase("n")) {
                        System.out.println("Goodbye");
                        finish = true;
                    } else {
                        System.out.println("Invalid selection");

                    }
                } while (finish != true);
                break;
            case 4:
                System.out.println("Goodbye");
                break;
            default:
                System.out.println("Invalid selection");
            }

        } catch (java.util.InputMismatchException e) {
            System.err.println("Please use numbers");
            scanner2.nextLine();
        }

    } while (game != 4);
    scanner2.close();
}

Aucun commentaire:

Enregistrer un commentaire