1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
DanielleElmas [232]
3 years ago
13

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.
Computers and Technology
1 answer:
Mashcka [7]3 years ago
7 0

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.

You might be interested in
The valence electron configurations of several atoms are shown. how many bonds can each atom make without hybridization? 2s^2 2p
Pie
Answer: This element needs 3 electrons to complete its octet state

Why?:
since the electrons in 2s2 are already paired they cannot form a bond pair with electrons from other elements without hybridisation however the 3 electrons in 2p3 are unpaired so can form bond pairs with electrons from other elements without hybridisation.

Hope this helps!
3 0
4 years ago
Write a program that asks the user for three strings. Then, print out whether the first string concatenated to the second string
ipn [44]

Answer:

import java.util.Scanner;

public class num5 {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter The First String");

       String str1 = in.next();

       System.out.println("Enter The Second String");

       String str2 = in.next();

       System.out.println("Enter The Third String");

       String str3 = in.next();

       String oneAndTwo = str1+str2;

       if(str3.equals(oneAndTwo)){

           System.out.println(str1+" + "+str2+" is equal to "+str3+"!");

       }

       else

           System.out.println(str1+" + "+str2+" is not equal to "+str3+"!");

   }

}

Explanation:

  • Implemented in Using Java Programming Language
  • Import Scanner Class to prompt and receive users' input
  • Create three string variables and store the three values entered by the user (str1, str2 and str3)
  • Concatenate str1 and str2 using the + operator and assign to a new variable
  • Use the if statement with Java's .equals() method to check for equality of the new string with the third string
  • Print the appropriate message if the equal or not
8 0
3 years ago
The ____ loop can be used to process each element of an array.?
salantis [7]
The "For each" loop can be used to process each element of an array.?
4 0
3 years ago
How to you put a color into a cell? <br> a.Shading<br><br> b.Fill<br><br> c.Color<br><br> d.Insert
Thepotemich [5.8K]
The answer is c) Color 
hope i could help u
6 0
3 years ago
What is the difference between a rendering engine and a browser engine?
melomori [17]
A rendering engine is the engine that a browser uses to render the html (i.e. gecko vs webkit).
A user agent is how a browser identifies itself (i.e firefox 3.5).
4 0
3 years ago
Read 2 more answers
Other questions:
  • An app for a hair styling stop needs a form for the various services that they offer. What control should be used to display all
    6·1 answer
  • Whats the difference between firms that provide services and firms that sell products?
    7·2 answers
  • How to transfer photos from lg tablet to computer?
    15·1 answer
  • Which is a tool that allows you to copy formatting from one place and apply it to other places?
    9·1 answer
  • Which of these is an off-site metric for social media marketing?
    8·2 answers
  • Write the name of the tab, command group, and icon that is used to create multilevel lists.
    12·1 answer
  • When is padding required for a structure type variable? Group of answer choices When the structure contains a word-type variable
    9·1 answer
  • Elaboren un texto acerca de una caracteristica o<br>componente de algun sistema tecnologico.<br>​
    15·1 answer
  • Seneca has just applied conditional formatting and realizes that she has made a mistake. Which action should she take to fix the
    12·2 answers
  • Cuales son las intenciones de la tecnología para el desarrollo social y calidad de vida
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!