**Java Basic Program**

➤ Hello World program

➤ Java Addition Program

➤ Average of 2 Numbers

➤ Average of 3 Numbers

➤ Simple Interest in Java

➤ Compound Interest

➤ Display ASCII Value

➤ Area of Circle Java

➤ Area of Rectangle

➤ Java Area of Triangle

➤ Java Swap 2 Numbers

➤ Distance b/w 2 Points

**Java Flow Control Program**

**Java Array Programs**

**Java String Programs**

**Dice Roll Program Java** | In this post, we will write a Java program to roll a dice or multiple dice at a time. Dice is a cube having 6 faces each faces is denoted by numbers from 1 to 6, when you roll a dice you get any of these numbers.

We are about to write the program for it in Java programming language. To know more observe the below examples:-

1) Roll a dice:

Outcome: 4

2) Roll a dice:

Outcome: 6

We can also roll multiple dice at a time. Example for rolling two dice at a time.

1) Number of dice: 2

Roll the dice.

Outcome: 2, 6

2) Number of dice: 2

Roll the dice.

Outcome: 1, 6

As there are only 6 numbers in the dice there is no possibility of getting any numbers more than 6.

## Dice Roll Program Java Code

Now let us see the Java dice roll program. It is a very simple Java program using the Random class to generate an integer number between 0 to 5. In the generated value 1 is added.

```
import java.util.Random;
public class Main {
public static void main(String args[]) {
Random random = new Random();
System.out.print("The value on dice is: "
+ (random.nextInt(6) + 1));
}
}
```

Output:-

The value on dice is: 5

The value on dice is: 3

The value on dice is: 6

## Roll Two Dice Java Program

The below dice roll program Java not only can roll a single dice, but it can also roll multiple dice at a time. For rolling, we are taking the help of the Random class defined in java.util package. The nextInt() method of the Random class will generate a new digit between 1 and 6.

**Java Program for Rolling Two Dice**

```
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
System.out.print("Enter the number of dice (>0): ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
while (num <= 0) {
System.out.println("The number of dice can't"+
" be 0 or -ve.");
System.out.print("Enter number of dice: ");
num = scan.nextInt();
}
Random rand = new Random();
do {
System.out.println("The values on dice are: ");
for (int i = 0; i < num; i++) {
System.out.println(rand.nextInt(6) + 1);
}
System.out.print("Do you want to roll the" +
" dice again? true/false: ");
} while (scan.nextBoolean() == true);
scan.close();
}
}
```

Output:-

Enter the number of dice (>0): 1

The values on dice are:

5

Do you want to roll the dice again? true/false: true

The values on dice are:

5

Do you want to roll the dice again? true/false: true

The values on dice are:

6

Do you want to roll the dice again? true/false: false

Enter the number of dice (>0): 0

The number of dice can’t be 0 or -ve.

Enter number of dice: -5

The number of dice can’t be 0 or -ve.

Enter number of dice: 1

The values on dice are:

1

Do you want to roll the dice again? true/false: false

Enter the number of dice (>0): 2

The values on dice are:

1

1

Do you want to roll the dice again? true/false: true

The values on dice are:

1

6

Do you want to roll the dice again? true/false: true

The values on dice are:

3

5

Do you want to roll the dice again? true/false: false

The above roll two dice java program is able to roll multiple dice simultaneously. Also see:- Hidden Word Java Program

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!