Ask Question
22 November, 02:29

Write a program that simulates rolling one die using the following steps: 1. Prompt the user for the number of sides on the die. 2. "Roll" the die three times by generating a random number between 1 and the number of sides (inclusive). 3. Keep track of the running sum of the rolls for the die and output the sum and average for the three rolls at the end. 4. You can set up one integer variable named roll and reuse it with each roll of the die. You will also need a variable named total, initialized to zero.

+4
Answers (1)
  1. 22 November, 03:14
    0
    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.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a program that simulates rolling one die using the following steps: 1. Prompt the user for the number of sides on the die. 2. "Roll" ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers