Dice Rolling Java Program

I'm making a dice rolling game! 2 dice will be rolled and 2 random numbers between 1-6 will be generated. The sum will be taken from the 2 numbers and used to decide what is next. If user's sum is 2,3,12 then they lose. If the sum is 7,11 then they win. If sum is 4,5,6,8,9,10 then the program automatically rolls the dice again until the user wins or loses. Also, after every sum displayed, underneath, display the amount of games they have won/lost. Here is my code so far:

//import java.util.Scanner;
public class Lab5 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        //variables
        int dice1;
        int dice2;


        //Call the welcome method
        welcome();

        //fetch random numbers


        /*
         * **************************************************************
         *welcome method
         *welcome user
         *no parameters
         *no return
         ****************************************************************
         */
    }
    public static void welcome() {
        System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!");{
    }

    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 LOSE :("); }
    else if(sum==7 || sum==11) { 
    System.out.println("Woah!!! With a " + sum + " You WIN!!!!!!!"); } 
    else{ 
    if(sum==4 ||sum==5 ||sum==6 ||sum==8 ||sum==9 ||sum==10) 
    dice1=(int)(Math.random()*6+1);
    dice2=(int)(Math.random()*6+1);}
    int roll2 = dice1 + dice2;}
    System.out.print("You rolled "+roll2+"  ");{
    while (roll2 !=7){
    if (roll == roll2);{
    System.out.println("You Win !");
    break;
    }else{

        }
    }
}}

I'm not sure how to display the games the user won/lost or how to make the program roll the dice again if they did not win/lose.

4

You should use a while loop: The dice are rolled again and again until the player has won or lost (then, break ends the while loop).

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 LOSE :(");
        break;
    } else if(sum==7 || sum==11) { 
        System.out.println("Woah!!! With a " + sum + " You WIN!!!!!!!");
        break; 
    }

    // If you wanted, you could wait here for the user to confirm (e.g. with a key press)
}
0
Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest