I am fairly new to Java programming and I am having problems with creating a program with these requirements:

1. Create a program to simulate the roll of 2 – six sided dice.
2. Test to see if the dice are equal.
3a. If they are equal print “You win! You rolled a pair.
3b. If they are not equal prompt the user if they would like to continue.
4. Use 1 to continue and 2 to end the program.
5. When the program ends print a statement one of these statements:
You won after 9 rolls!
Or
You chose to end the game without winning after 8 tries.

If there is any help out there it would be greatly appreciated. I may be way off but below is what I currently have:

public class Dice {
        
    public static void main(String[] args) {

    // Welcome message.

        System.out.println ("Welcome to The Dice Roller Game!");
        
   // Roll the dice.
    	
    		int die1, die2;
    		int rollcount;
    		rollcount = 0;
			
		do {
			die1 = (int)(Math.random()*6) + 1;
            die2 = (int)(Math.random()*6) + 1;
            rollcount++;
		}	while ( die1 != die2 );{
					System.out.println("Would you like to roll again? 1 = Yes, 2 = No");
					Scanner keyboard = new Scanner(System.in);
					int userinput=keyboard.nextInt();
						}
										
		System.out.println("You won in " + rollcount + " rolls.");
		
	}
}

Thank you for any help!

Recommended Answers

All 3 Replies

public class Dice {
        
    public static void main(String[] args) {

    // Welcome message.

        System.out.println ("Welcome to The Dice Roller Game!");
        
   // Roll the dice.
    	
    		int die1, die2;
    		int rollcount;
    		rollcount = 0;
		int wins = 0;	

Scanner keyboard = new Scanner(System.in);
int userinput=-1;

		do {
			die1 = (int)(Math.random()*6) + 1;
            die2 = (int)(Math.random()*6) + 1;
            rollcount++;
            if (die1 == die2) {
                  System.out.println("You won in " + rollcount + " rolls.");
                  rollcount = 0;
                 wins++;
                 System.out.println("Would you like to play again?");
                  userinput=keyboard.nextInt();
            } else {
                 System.out.println("You didn't roll pairs.");
                System.out.println("Try Again?");
		userinput=keyboard.nextInt();
            }
		}	while ( userinput==1 );

System.out.println("Wins: "+wins);
	}
}

You can change the above code according to your needs

Thank you very much. That help clear up a lot.

Can you show me how to make a dice game using 5 dice and a total of three rolls? Think of the game Yahtzee - same thing. My grand kids keep losing the dice. If I could do this then i can put it on my website and they can play it with the virtual dice on my tablet. Thanks

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.