Results 1 to 7 of 7
  1. #1
    phughue is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Post 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Help Please : Finding average of dice rolls

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    phughue is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    phughue is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default 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 08:22 PM. Reason: Added / in ending code tag

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    kire9dk is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default 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.

Similar Threads

  1. Finding Min/Max/Average/Sum in an Array.
    By tonythekid92 in forum New To Java
    Replies: 2
    Last Post: 09-17-2013, 05:45 AM
  2. Replies: 3
    Last Post: 10-16-2012, 11:47 PM
  3. Problem in Finding the Mode of Dice Roll Outcomes? Help?
    By Kratos321 in forum Advanced Java
    Replies: 1
    Last Post: 05-11-2011, 04:21 AM
  4. finding average in 2d arrays
    By the beginner in forum New To Java
    Replies: 18
    Last Post: 02-24-2011, 05:32 AM
  5. need some help finding average on java
    By jtw0812 in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 05:26 AM

Posting Permissions

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