How to ask if user wants to continue the game after each die roll in java using netbeans

Hi guys, I need help with this question: The dice game is played with two hex dice. The user playing the game will flip two dice and create two random numbers between one and six. The sum of the two numbers will be used to take the next step. If the total is 2.3 or 12, the player wins. If the sum is 7 or 11, then he / she loses. If the sum is 4, 5, 6, 8, 9, or 10, the program will automatically flip the dice until the player wins or loses. After each dice roll, the player will be prompted to enter an entrance. The player must decide whether to continue playing or not. The number of games won and lost must also be displayed after each dice roll. I was able to migrate the first part, but couldn't figure out how to query the user,whether they want to continue or how many games they won / lost

public class DiceGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        while (true) {
            int dice1=(int)(Math.random()*6+1);
            int dice2=(int)(Math.random()*6+1);
            int sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum==2 || sum==3 || sum==12) {
                System.out.println("Sorry with a " +  sum  + " you loose :(");
                break;
            }
            else if(sum==7 || sum==11) {
                System.out.println("With a " + sum + " you win :)");
                break;
            }
        }
    }

}

      

+3


source to share


4 answers


The scanner can be used to ask the user to ask if he wants to continue or not.

You can even track no. once the pieces have been rolled.

import java.util.Scanner;

public class DiceGame {

    public static int attempt = 1;
    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int dice1 = (int) (Math.random() * 6 + 1);
        int dice2 = (int) (Math.random() * 6 + 1);
        int sum = dice1 + dice2;

        while (true) {
            System.out.println();
            System.out.println("Rolling dice for " + attempt + " time!");

            dice1 = (int) (Math.random() * 6 + 1);
            dice2 = (int) (Math.random() * 6 + 1);
            sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum == 2 || sum == 3 || sum == 12) {
                System.out.println("Sorry with a " + sum + " you loose :(!");
                System.out.println();
                break;
            } else if (sum == 7 || sum == 11) {
                System.out.println("With a " + sum + " you win :)!");
                System.out.println();
                break;
            }
            System.out.println();
            System.out.println("Do you wish to continue? Press 'y' for YES or ANY key for EXIT");
            if (!scanner.next().equalsIgnoreCase("y")) {
                break;
            }
            attempt++;
        }
        System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
    }
}

      

EDIT:

If you want to roll the cubes automatically when the sum is 4 5 6 8 9 10, then you no longer need a scanner, that is, user input, whether to continue or not.



Here is a solution for the same.

public class DiceGame {

    public static int attempt = 1;

    public static void main(String[] args) {

        int dice1 = 0;
        int dice2 = 0;
        int sum = 0;

        while (true) {
            System.out.println();
            System.out.println("Rolling dice for " + attempt + " time!");

            dice1 = (int) (Math.random() * 6 + 1);
            dice2 = (int) (Math.random() * 6 + 1);
            sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum == 2 || sum == 3 || sum == 12) {
                System.out.println("Sorry with a " + sum + " you loose :(!");
                System.out.println();
                break;
            } else if (sum == 7 || sum == 11) {
                System.out.println("With a " + sum + " you win :)!");
                System.out.println();
                break;
                // this will roll the dices automatically
                // when sum is 4, 5, 6, 8, 9 or 10
            } else {
                System.out.println();
                System.out.println("With " + sum + " dices are rolled again automatically!!");
                attempt++;
            }
        }
        System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
    }
}

      

Run example

Rolling dice for 1 time!
Roll: total = 4

With a 4, dices are rolled again automatically!!

Rolling dice for 2 time!
Roll: total = 6

With a 6, dices are rolled again automatically!!

Rolling dice for 3 time!
Roll: total = 2
Sorry with a 2 you loose :(!

Thanks for playing dice game, you rolled the dice 3 times!

      

+3


source


You can use the java Scanner class to prompt the user for input.

Inside the while loop, ask the user for input. You may need to change the condition of the while loop.

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

      



Based on user input, use the "brake" to exit the loop. This should do it.

Link to scanner

+1


source


I created a method playAgainMessage

that returns an int. This decides if the user will play again. Check it:

import java.util.Scanner;

public class DiceGame {
    private static final int PLAY = 1;
    private static final int DO_NOT_PLAY = 2;
    private static int choice = 1;

    public static void main(String[] args) {
        while (choice == PLAY) {
            int dice1=(int)(Math.random()*6+1);
            int dice2=(int)(Math.random()*6+1);
            int sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum==2 || sum==3 || sum==12) {
                System.out.println("Sorry with a " +  sum  + " you loose :(");
                choice = playAgainMessage();
            }
            else if(sum==7 || sum==11) {
                System.out.println("With a " + sum + " you win :)");
                choice = playAgainMessage();
            }
        }
        if (choice == DO_NOT_PLAY) {
            System.out.println("Good bye...");
        }
    }

    private static int playAgainMessage() {
        Scanner input = new Scanner(System.in);
        System.out.println("Would you like to play again? '1' for yes, '2' for no");
        int choice = input.nextInt();
        input.close();
        if(choice == PLAY){
            System.out.println("Rolling...");
            return PLAY;
        } else {
            return DO_NOT_PLAY;
        }
    }
}

      

You can call the method playAgainMessage

whenever you want, which makes it nice and easy to read.

This will continue until the user enters something other than "1". Try running it to see the exact result.

+1


source


You can use a loop do{}while();

, it will play once and then ask the user again:

Scanner sc = new Scanner(System.in);
String input = "";
do {
    int // ...

    System.out.println("Roll: total = " + sum);

    if (sum == 2 || sum == 3 || sum == 12) {
        // ...
    } else if (sum == 7 || sum == 11) {    
        // ...
    }

    System.out.println("If you want to continue, write 'y' : ");
    input = sc.nextLine();
} while (input.equalsIgnoreCase("y"));

      

+1


source







All Articles