I have a dice game to need to creat that plays until the total of the two die equals the users number, or the two die roll 3 total times. I also need to display the die after each roll. Something like this:
Six: Five: and so on...
* * * *
* * *
* * * *
I have gotten it roll stop rolling after 3 times but I'm not sure about the rest.

import java.util.Scanner;  //Needed for Scanner class

public class Dice
{
	public static void main(String[] args)
	{
		int user;  		 //Input from user between 2 and 12
		int die1;  		 //Roll of die 1
		int die2;       //Roll of die 2		
		int total=0;    	 //Total of the two die rolled
		int roll = 0;	 //number of rolls for increment
		
				
		//Create a Scanner object for keyboard input.
		Scanner keyboard = new Scanner(System.in);
				
		//Get the number entered by user.
		System.out.print("Please enter a number between 2 and 12. ");
		user = keyboard.nextInt();
	
		do {
		//roll die.
		die1 = (int)(Math.random()*6) + 1;     //first die rolled
		die2 = (int)(Math.random()*6) + 1;		//second die rolled
		total = die1 + die2;							//total of two rolls
		roll = roll + 1;
		
				
		//print out the result
		System.out.println("Your first roll is a: " + die1);
		System.out.println("Your second roll is a: " + die2);
		System.out.println("Your total is: " + total);
		System.out.println();
		
                            }while(roll<3);
		
}				
}

Recommended Answers

All 10 Replies

You need to take user provided input, check it if it is really a number (give warning if not and return back to step of number insert), after each roll you should compare user value with dices roll and see if they equal. In case they do not equal make another roll as long it does not exceed total roll of 3 when you close the game

I got those parts, I'm not sure how to display the actual dice after each roll.

Now you not making any sense.

do {
	//roll die.
	die1 = (int)(Math.random()*6) + 1;     //first die rolled
	die2 = (int)(Math.random()*6) + 1;	//second die rolled
	total = die1 + die2;			//total of two rolls
	roll = roll + 1;
		
				
	//print out the result
	System.out.println("Your first roll is a: " + die1);
	System.out.println("Your second roll is a: " + die2);
	System.out.println("Your total is: " + total);
	System.out.println();
		
}while(roll<3);

The red marked part is where you display your dices and around that are you should make comparison to user input. You need clearly explain what you want to do or we cannot help you

After each of the die is rolled I have to display something like this:

if the roll is say a 6, i display

* *
* *
* *

After each of the die is rolled I have to display something like this:

if the roll is say a 6, i display

* *
* *
* *

You can write a function like this:

void DisplayRollWithAsterisks (int roll)

Decide what the asterisks look like for each value or roll (1 - 6) and use System.out.print and/or System.out.println to display the asterisks.

how do I make the program stop if the user is a winner(user=total). I got it to stop after 3 sets of rolls, but I need it to stop before the 3 sets if the user enters the right number. Also, I am suppose to make the program so the user can keep playing until he answers no. I dont know why I'm so confused with this.
Thanks for all the help

how do I make the program stop if the user is a winner(user=total). I got it to stop after 3 sets of rolls, but I need it to stop before the 3 sets if the user enters the right number. Also, I am suppose to make the program so the user can keep playing until he answers no. I dont know why I'm so confused with this.
Thanks for all the help

Look at peter budo's last post and change the while condition to check for whether the roll is the same as what the user entered in addition to the number of rolls.

while(roll<3);

For the part where the user is prompted till he/she says no, you can have another while loop surrounding the while loop you already have where the user is prompted and the game is exited when the user no longer wants to play. Get it to work for ONE game before tackling that part and post your updated code.

So I have most of it working except for the dice displaying visually. I still can figure out why it wont end when the user enters n for no.

public static void main(String[] args)
	{
		int user=0;  		 //Input from user between 2 and 12
		int die1;  		    //Roll of die 1
		int die2;          //Roll of die 2		
		int total=0;    	 //Total of the two die rolled
		int roll=0;	       //number of rolls for increment
						
		System.out.println("This is my die rolling program");
				
		//Create a Scanner object for keyboard input.
		Scanner keyboard = new Scanner(System.in);
				
				
		//Get the number entered by user.
	
		while(user<2 || user>12){
		System.out.print("Please enter a number between 2 and 12. ");
		user = keyboard.nextInt();
		System.out.println();
		}		
		do{			
		die1 = (int)(Math.random()*6) + 1;     //first die rolled
		die2 = (int)(Math.random()*6) + 1;		//second die rolled
		total = (die1 + die2);						//total of two rolls
		roll=roll+1;
				
		//print out the result
		System.out.println("Your first roll is a: " + die1);
		System.out.println("Your second roll is a: " + die2);
		System.out.println("Your total is: " + total);
		System.out.println();
		if(user==total)
			System.out.println("You are a winner! ");
				
		}while (roll<3 && user!=total);
			
								
		
	}

}

So I have most of it working except for the dice displaying visually. I still can figure out why it wont end when the user enters n for no.

Typo? do you mean "can't" instead of "can"?

Why would it end? You never ask the user whether he/she wants to play again, nor is there anywhere in the code where a variable is compared to 'y' or 'n'.

Okay so I think I have got it, but I'm missing something somwhere because it wont display the die in my switch.

import java.util.Scanner;  //Needed for Scanner class

public class Dice
{
	public static void main(String[] args)
	{
		//Initialize variables
		int user, die1, die2, total, play;
		String play2; 		 //Input from user between 2 and 12
								
		System.out.println("This is my die rolling program");
				
		//Create a Scanner object for keyboard input.
		Scanner keyboard = new Scanner(System.in);
		Scanner keyboard2 = new Scanner(System.in);		
				
		//Does the user want to play?
		play=3;
		while(play<1 || play>2){
			System.out.println("If you would like to play the dice game Press Y for Yes, N for No");
			play2 = keyboard.nextLine();
				if(play2.equalsIgnoreCase("Y"))
					play = 1;
				else
					play = 2;
				}
			
			do{
			user=100;
			while(user<2 || user>12){
			System.out.println("Enter a number between 2 and 12.");
			guess=keyboard.nextInt();
			}
		for(int i=1; i<3; i++){	
			die1 = (int)(Math.random()*6) + 1;     //first die rolled
			die2 = (int)(Math.random()*6) + 1;		//second die rolled
			total = (die1 + die2);						//total of two rolls
		}		
		//print out the result
		switch(die1){
		case 1:
			System.out.println("     \n" + 
							       "  *  \n" +
							       "     \n");
			break;
		case 2:
			System.out.println(" *   \n" + 
							    	 "     \n" +
							       "     *\n");
			break;
		case 3:
			System.out.println(" *   \n" + 
							       "   *  \n" +
							       "     *\n");
			break;
		case 4:
			System.out.println(" *   * \n" + 
							          "       \n" +
							          " *   * \n");
			break;
		case 5:
			System.out.println(" *   * \n" + 
							       "   *   \n" +
							       " *   * \n");
			break;
		case 6:
			System.out.println(" *   * \n" + 
							       " *   * \n" +
							     	 " *   * \n");
			break;
		}
	 	System.out.println("The total is: " + total + "\n");
		
			if(user==total){
				System.out.println("Winner! \n");
				i=4;
				}
			else if(i>3){
				System.out.println("Loser \n");
			}
		System.out.println("Want to play again? Y for Yes, N for No?");
		play2 = keyboard2.nextline();
			if(play2.equalsIgnoreCase("Y"))
				play=1;
			else
				play=2
			}while(play==1);
		
		}		
		
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.