Answer:
The program to the given question can be given as:
Program:
//import package.
import java.util.*;
//define class.
public class Main
{
public static void main(String ar[]) //define main method.
{
Scanner ob = new Scanner(System.in); //creating scanner class object.
Random ran = new Random(); //creating random class object.
int roll1,roll2,roll3,number,total=0; //define variable.
float avg=0;
System.out.print("Enter the sides= "); //print message.
number = scanner.nextInt(); //input number by user.
roll1 = ran.nextInt(number++); //using random function.
roll2 = ran.nextInt(number++);
roll3 = ran.nextInt(number++);
System.out.print("First roll= "); //print message
System.out.println(roll1); //print value.
System.out.print("Second roll= ");
System.out.println(roll2);
System.out.print("Third roll= ");
System.out.println(roll3);
System.out.print("total roll= ");
total=roll1+roll2+roll3; //add all number.
System.out.println(total);
System.out.print("Average roll= ");
avg=total/3; //Average of the number.
System.out.println(avg);
}
}
Output:
Enter the sides= 4
First roll= 2
Second roll= 3
Third roll= 5
total roll= 10
Average roll= 3.0
Explanation:
In the above program first, we import the package form the user input and random number. In java, there are many packages but we use only the util package because in that package both the class like scanner class and random class are inbuilt. Then we declare the class that name is Main in that class we define the main function. In the main function, we create both class object (scanner class and random class). Then we take input a number from the user and pass into the random function. The random function is used to give a unique number and we pass the number into the random function that is increasing by post-increment operator i.e (X++). Then we print all the rolls and total by add all the roll and at the last, we print an average of it.