1. Member
Join Date
Nov 2013
Posts
3
Rep Power
0

## Help Please : Finding average of dice rolls

Problem Description
In this lab you will create and use a dice rolling simulator by using Java's random number generator:
int die = (int)(Math.random() * 6 + 1);
will result in either 1, 2, 3, 4, 5, or 6. Make sure you understand why.
Your program must do the following:

1. Prompt the user to enter the number of times to roll a six-sided die. The user can roll a die
no more than 10 times. If the user inputs a value less than 1 or greater than 10, the user should
be prompted again until an "valid" input value is read.

2. Use a loop that iterates the appropriate number of times and does the following:
 "rolls the die" by getting a random value between 1 and 6
 adds the roll value to a variable that keeps track of the sum of all roll values
 outputs the roll value
 increments a counter variable used to keep track of how many times the loop has iterated

Sample run(s):
Enter number of rolls (1-10): 5
Roll: 5
Roll: 3
Roll: 5
Roll: 3
Roll: 6
Average roll: 4.4
-------------------------
Enter number of rolls (1-10): 0
Invalid entry!
Enter number of rolls: 11
Invalid entry!
Enter number of rolls: 10
Roll: 1
Roll: 6
Roll: 1
Roll: 3
Roll: 4
Roll: 2
Roll: 5
Roll: 1
Roll: 3
Roll: 5
Average roll: 3.1

3. Member
Join Date
Nov 2013
Posts
3
Rep Power
0

## Re: Help Please : Finding average of dice rolls

import java.util.Scanner;
public class Lab08
{
public static void main (String[] args)
{
Scanner stdIn = new Scanner(System.in);

int roll;
System.out.print("Enter number of rolls (1-10):");
roll = stdIn.nextInt();

while (!(roll<=10 && roll>=1))

{
System.out.print("Enter number of rolls (1-10):");
roll = stdIn.nextInt();

}

for (roll)
{
int die = (int)(Math.random() * 6 + 1);

}
stdIn.close();
}

}

4. ## Re: Help Please : Finding average of dice rolls

Can you post the program's output and add some comments showing what is wrong with it.

[code]
[/code]
to get highlighting and preserve formatting.

5. Member
Join Date
Nov 2013
Posts
3
Rep Power
0

## Re: Help Please : Finding average of dice rolls

Java Code:
```import java.util.Scanner;
public class Lab08
{
public static void main (String[] args)
{
Scanner stdIn = new Scanner(System.in);

int roll;
System.out.print("Enter number of rolls (1-10):");
roll = stdIn.nextInt();

while (!(roll<=10 && roll>=1))

{
System.out.print("Enter number of rolls (1-10):");
roll = stdIn.nextInt();

}

for (roll)
{
int die = (int)(Math.random() * 6 + 1);

}
stdIn.close();
}

}```
Last edited by Norm; 11-01-2013 at 07:22 PM. Reason: Added / in ending code tag

6. ## Re: Help Please : Finding average of dice rolls

Can you post the program's output and add some comments showing what is wrong with it.

If there are error messages, copy the full text and paste it here.

7. Member
Join Date
Sep 2009
Posts
16
Rep Power
0

## Re: Help Please : Finding average of dice rolls

I am having troubles to understand what is happening here, but if you need average numbers,
then you will need sum of all numbers and then to divide to the number of elements, in your case number of trows.

Average - Wikipedia, the free encyclopedia.

So if roll is the number of rolls, then you will have (die_1+die_2+...die_roll)/roll. die_1 is the value of the die into the first roll.

Java Code:
```int sum =0;
for(int i = 0; i < roll; i++){
die = ....;
sum += die;}

double avg = ((double)sum)/roll.```
You have very strange for loop. Some new syntax.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•