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:
New Features in Java 12
Java Program to Implement CopyOnWriteArrayList API
Java Program to Implement ConcurrentHashMap API
Compact Strings in Java 9
Optional trong Java 8
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Using Spring @ResponseStatus to Set HTTP Status Code
Collect a Java Stream to an Immutable Collection
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Từ khóa this và super trong Java
Guide to the Volatile Keyword in Java
Changing Annotation Parameters At Runtime
How to Get the Last Element of a Stream in Java?
Hướng dẫn Java Design Pattern – Null Object
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Guide to Selenium with JUnit / TestNG
Guide to UUID in Java
Removing all Nulls from a List in Java
Java Program to Implement Knight’s Tour Problem
Hướng dẫn Java Design Pattern – Flyweight
Functional Interfaces in Java 8
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Spring @RequestMapping New Shortcut Annotations
Tránh lỗi NullPointerException trong Java như thế nào?
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Spring Security and OpenID Connect
Java Stream Filter with Lambda Expression
Configure a Spring Boot Web Application
Java Program to Perform Complex Number Multiplication