Java Program to Emulate N Dice Roller

This is the java program for emulating N dice roller. This can be achieved using random numbers. User can also select how many dice in a game.

Here is the source code of the Java Program to Emulate N Dice Roller. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a sample program to emulate n dice roller
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Emulation_N_Dice 
  7. {
  8.     public static void main(String args[])
  9.     {
  10.         System.out.println("Enter the number of dice: ");
  11.         Scanner sc = new Scanner(System.in);
  12.         int n = sc.nextInt();
  13.         Random rand = new Random();
  14.         do 
  15.         {
  16.             System.out.println("The values on dice are: ");
  17.             for(int i=0; i<n; i++)
  18.                 System.out.println(rand.nextInt(6)+1);
  19.  
  20.             System.out.println("Continue: true/false");
  21.         } while (sc.nextBoolean() == true);
  22.         sc.close();
  23.     }
  24. }

Output:

$ javac Emulate_N_Dice.java
$ java Emulate_N_Dice
Enter the number of dice: 
6
The values on dice are: 
1 1 1 2 2 6
Continue: true/false
true
The values on dice are: 
6 5 2 5 5 1
Continue: true/false
false

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.