Skip to content
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

Simplified Learning Blog

Learning made easy

  • Java
    • Core Java Tutorial
    • Java 8
    • What is Rest API in java
    • Spring Framework
    • Type Casting in Java | 2 types Implicit and explicit casting
    • Spring Boot Tutorial
      • Spring Boot Rest API Example complete guide
    • Top 50 Java Interview Questions
    • JUnit 5 Tutorial
      • Assertall in JUnit 5
      • Assertions in JUnit 5
    • Java Thread Tutorials
      • How to create thread in Java
      • Multithreading in java
      • Daemon Thread in Java | How to create daemon thread in java
      • Top 40+ Multithreading interview questions
  • AWS
    • What is AWS (Amazon Web Services)
    • AWS IAM (Identity and Access Management)
    • AWS SNS | What is SNS
    • What is SQS | AWS SQS (Simple Queue Service)
    • What is AWS Lambda
    • Top 10 AWS Lambda interview questions
  • Java Codes
  • Software Architecture
    • Software Architecture Performance
    • Performance Principles of Software Architecture
    • System Performance Objective
  • Spring Boot Tutorial
  • Tools
    • JSON Formatter and Validator
  • Tech Blogs
    • Java 21 New Features
    • Is Java Dead? Is java dead, 2023 ?
    • New Features in Java 17
  • Toggle search form

Java dice roll program

Posted on April 4, 2023February 27, 2024 By Admin No Comments on Java dice roll program

In this blog tutorials, we will see how to implement using java dice roll program.

Table of Contents

Toggle
  • Code Java Dice Roll Program
    • Code Explanation
  • What is dice roll ?
  • Advanced Java Dice Roll Program
    • Code advance dice roll program
    • Code Explanation
  • Conclusion

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 ?

Java dice roll program
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 scanner 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

Related

Java Codes Tags:Java dice roll program, java program, java program for rolling dice

Post navigation

Previous Post: How to read Excel File using jxl
Next Post: Series program in java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • PDF to JSON Convertor
  • System Performance Objective
  • Performance Principles of Software Architecture
  • Calculate date of birth from age in jquery
  • How to convert excel to PDF using java

Recent Comments

No comments to show.

Copyright © 2025 Simplified Learning Blog.

Powered by PressBook WordPress theme