Skip to content

Instantly share code, notes, and snippets.

@khatchad
Last active August 29, 2015 14:18
Show Gist options
  • Save khatchad/7657a9cbb9b9628febad to your computer and use it in GitHub Desktop.
Save khatchad/7657a9cbb9b9628febad to your computer and use it in GitHub Desktop.
This program simulates the rolling of dice.
import java.util.Scanner;
import java.util.Random;
/**
* This program simulates the rolling of dice.
*/
public class RollDice
{
public static void main(String[] args)
{
String again = "y"; // To control the loop
// Create a Scanner object to read keyboard input.
Scanner keyboard = new Scanner(System.in);
// Create a Random object to generate random numbers.
Random rand = new Random();
// Simulate rolling the dice.
while (again.equalsIgnoreCase("y"))
{
System.out.println("Rolling the dice...");
int die1 = rand.nextInt(6) + 1;
int die2 = rand.nextInt(6) + 1;
System.out.println("Their values are:");
System.out.println(die1 + " " + die2);
System.out.print("Roll them again (y = yes)? ");
again = keyboard.nextLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment