# Help Please : Finding average of dice rolls

• 11-01-2013, 07:26 PM
phughue
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
• 11-01-2013, 07:30 PM
Norm
Re: Help Please : Finding average of dice rolls
• 11-01-2013, 07:36 PM
phughue
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();
}

}
• 11-01-2013, 07:50 PM
Norm
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.
• 11-01-2013, 08:00 PM
phughue
Re: Help Please : Finding average of dice rolls
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();         }                         }```
• 11-01-2013, 08:23 PM
Norm
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.
• 11-03-2013, 09:39 AM
kire9dk
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.

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. :(happy):