In this blog tutorials, we will see how to implement using java dice roll program.
Code Java Dice Roll Program
package com.slb.java;
import java.util.Random;
public class DiceRollSLB {
public static void main(String[] args) {
Random random = new Random();
int dice1 = random.nextInt(6) + 1;
int dice2 = random.nextInt(6) + 1;
System.out.println("Dice 1: " + dice1);
System.out.println("Dice 2: " + dice2);
}
}
OP: 1
Dice 1: 3
Dice 2: 5
OP: 2
Dice 1: 3
Dice 2: 6
OP: 3
Dice 1: 1
Dice 2:
Code Explanation
The above program calculates rolling of a specified number of dice with a specified number of sides on each die, and prints out the results of each roll. In above example we have rolled it 3 times that means we ran this above program 3 times.
What is dice roll ?

Rolling dices is a form of chance that involves rolling at least one die. Once rolled, the face value of the die will determine its outcome; and depending on how many sides there are on it, values ranging from 1 to 6 inclusive can be produced from that roll. Dice rolls are board games or gambling games in order to add an element of chance and uncertainty into gameplays.
Advanced Java Dice Roll Program
Code advance dice roll program
package com.slb.java;
import java.util.Random;
import java.util.Scanner;
public class AdvancedDiceRollSLB {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
int numDice, numSides;
boolean isValid = false;
// get number of dice and sides and then operate on the inputs
while (!isValid) {
System.out.print("Enter number of dice: ");
numDice = input.nextInt();
System.out.print("Enter number of sides on each die: ");
numSides = input.nextInt();
// check if inputs are proper valid ones
if (numDice > 0 && numSides > 0) {
isValid = true;
int sum = 0;
// roll each die and print the result on console
for (int i = 1; i <= numDice; i++) {
int roll = random.nextInt(numSides) + 1;
System.out.println("Die " + i + ": " + roll);
sum += roll;
}
// print the sum of dice
System.out.println("Total: " + sum);
} else {
System.out.println("Invalid input. Please enter positive integers.");
}
}
}
}
Output:
Enter number of dice: 6
Enter number of sides on each die: 2
Die 1: 2
Die 2: 1
Die 3: 1
Die 4: 2
Die 5: 1
Die 6: 1
Total: 8
Process finished with exit code 0
Run #2
Enter number of dice: 2
Enter number of sides on each die: 2
Die 1: 1
Die 2: 1
Total: 2
Process finished with exit code 0
Code Explanation
This program called AdvancedDiceRoll
AdvancedDiceRoll is an upgraded code version of a DiceRoll
application which is there above at the start. This program lets you roll a certain number of dice, with an exact amount of faces on every die and then prints the outcomes from each roll, as well as the total of the rolls.
The program makes use of its random
class to create random numbers for each die roll, and also the s
canner
class (used to scan data from the user) is used to obtain feedback from the person who is using it. Then, it asks the user to input numbers for the dice they want to roll as well as how many sides for the die. It it checks whether the input is valid (both inputs have to be integers that are positive).
When the inputs are valid, The program rolls the dies and outputs the result along with the total sum of all dies. In the event that inputs are incorrect, the program displays an error warning and asks the user to input valid data.
The program makes use of an endless loop to prompt the user to input data until input that is valid and ensures that this program can provide a an output that is valid.
Overall, Advanced Dice Roll program, a good program. AdvancedDiceRoll
program is an sophisticated and flexible variation of DiceRoll
code which allows users to choose the number of sides and dice of each die, and providing more detailed output.
Conclusion
In this blog, we have seen that we can use this Dice Roll program to calculate the dice roll chances that can be used as base logic in creating java based board games.
Checkout Java Tutorials here