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.
//This is a sample program to emulate n dice roller
import java.util.Random;
import java.util.Scanner;
public class Emulation_N_Dice
{
public static void main(String args[])
{
System.out.println("Enter the number of dice: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Random rand = new Random();
do
{
System.out.println("The values on dice are: ");
for(int i=0; i<n; i++)
System.out.println(rand.nextInt(6)+1);
System.out.println("Continue: true/false");
} while (sc.nextBoolean() == true);
sc.close();
}
}
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
Related posts:
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
How to Kill a Java Thread
Spring – Injecting Collections
Java Optional as Return Type
Filtering and Transforming Collections in Guava
Introduction to Apache Commons Text
Java Program to Implement Attribute API
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Implement Bit Array
Spring Boot - Bootstrapping
Initialize a HashMap in Java
Java Program to Solve the 0-1 Knapsack Problem
Guide to CountDownLatch in Java
Java Program to find the peak element of an array using Binary Search approach
Java Program to Implement Pollard Rho Algorithm
Java – Write an InputStream to a File
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java Program to Implement Sorted Doubly Linked List
How to Convert List to Map in Java
Static Content in Spring WebFlux
Mockito and JUnit 5 – Using ExtendWith
Migrating from JUnit 4 to JUnit 5
Spring Data JPA and Null Parameters
How to Read a File in Java
Spring MVC Async vs Spring WebFlux
Guide to Mustache with Spring Boot
Spring Boot Tutorial – Bootstrap a Simple Application
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Introduction to Thread Pools in Java
Java Program to Implement Queue using Two Stacks
Hướng dẫn sử dụng lớp Console trong java