Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Calculate median dice game HELP!

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculate median dice game HELP!

    Hello,

    I am a beginner programmer in Java. I got stuck with one of the exercises and do not understand what could be wrong.
    I cannot find out what is the issue with my median calculation....

    My program should roll one dice (1-6) number of times. The program shall then collect how many of each dice value was computed.
    The program should also calculate average value and median value. The output should look like:

    Number of rolls :

    xxx times of dice value 1
    xxx times of dice value 2
    xxx times of dice value 3
    xxx times of dice value 4
    xxx times of dice value 5
    xxx times of dice value 6

    Average:

    Median:

    Here is my code:

    import java.util.Arrays;
    import java.util.Scanner;
     
    public class DiceGame
    {
    public static void main (String[] args) 
    {
     
    Scanner inputReader = new Scanner(System.in);
     
    //Array or each dice value
    int[] number = new int[]{1, 2, 3, 4, 5, 6};
     
    //Array for collecting number of each dice value
    int[] counter = new int[6]; 
     
    //number of executions:
    int exec = 11;
     
    //Array to calculate median
    int[] medianArray = new int[exec];
     
    // Variables needed to calculate median and average values
    double sum = 0;
    double average = 0;
    double median = 0;
     
    //Prints how many executions 
    System.out.println("Number of rolls" + exec);
     
    for(int i =0 ;  i < exec ; i++)
    {
        //Roll the dice
        int dice=(int)(Math.random()*6);
     
        //For each roll count each value
        counter[dice]++;
     
        //For each roll store result in Array
        medianArray[i] = dice;
     
    }
     
    for(int j = 0 ; j < 6 ; j++)
    {
        System.out.println(counter[j]+" times of dice value "+number[j]);
        sum = sum + (counter[j] * number[j]);
    }
     
    average = sum / exec;
    System.out.println("Average " + average);
     
    // Sort medianArray:
    Arrays.sort (medianArray);
     
    //If number of elements are an even number:
    if (medianArray.length % 2 == 0)
    {
        median = (medianArray[medianArray.length/2] + 
                 medianArray[(medianArray.length/2) +1])
                 /2;
     
        System.out.println("Median "+ median);
    }
     
    //Otherwise the median value is in the middle:
    else
    {
        median = medianArray[((medianArray.length)/2)];
        System.out.println("Median "+ median);
    }
     
    }
    }

    Output looks like:

    Number of rolls11
    3 times of dice value 1
    0 times of dice value 2
    2 times of dice value 3
    2 times of dice value 4
    2 times of dice value 5
    2 times of dice value 6
    Average 3.5454545454545454
    Median 3.0

    The meedian value is not correct, it should be the 6th value in the sorted medianArray, in other words value 4.
    What could be wrong in my code?
    Last edited by prophecy; November 7th, 2020 at 12:27 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calculate median dice game HELP!

    Can you copy the program's output and paste it here so we can see what you are asking about?
    Please add comments to the output to describe what is wrong with it and show what the desired output is.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculate median dice game HELP!

    Quote Originally Posted by Norm View Post
    Can you copy the program's output and paste it here so we can see what you are asking about?
    Please add comments to the output to describe what is wrong with it and show what the desired output is.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Thanx, I edited my post.. hopefully my issue is more clear now.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calculate median dice game HELP!

    The meedian value is not correct,
    What is the correct formula for computing the median value? Does your code use that formula?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculate median dice game HELP!

    Quote Originally Posted by Norm View Post
    What is the correct formula for computing the median value? Does your code use that formula?
    I am storing all computed values in medianArray. Afterwards I am sorting the Array and the medianArray should look like:

    1 1 1 3 3 4 4 5 5 6 6

    Since the medianArray consists of index 11, the median valus is in the middle, hence:

    median = medianArray[((medianArray.length)/2)];
    System.out.println("Median "+ median);

    But if I instead had index of 10 the medianArray can look like following:

    1 1 1 3 3 4 4 5 5 6

    The median is the calculated by sum up the values in the middle and divide them by 2:

    (3 + 4) / 2 = 3,5

    median = (medianArray[medianArray.length/2] +
    medianArray[(medianArray.length/2) +1])
    /2;

    System.out.println("Median "+ median);

    But the latter don't work...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calculate median dice game HELP!

    the latter don't work
    Why do you think your value is wrong?

    Since the medianArray consists of index 11, the median valus is in the middle, hence:

    median = medianArray[((medianArray.length)/2)];
    Why isn't that code executed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculate median dice game HELP!

    Quote Originally Posted by Norm View Post
    Why do you think your value is wrong?

    Could you post what the correct formula is for computing the median?
    I fixed it at last....

    The issue was in the roll itself. with my code 'int dice=(int)(Math.random()*6) ;' I enabled 0 as a values as well... Adding +1 solved it:

    int dice=(int)(Math.random()*6) + 1;

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calculate median dice game HELP!

    Is that the only change that the code needs? I get this exception with that simple change:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member JasonCurtis's Avatar
    Join Date
    Mar 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculate median dice game HELP!

    Upon reviewing your code, I noticed a small error that might be causing the incorrect median value. In the if statement where you calculate the median for an even number of elements, change (medianArray.length/2) + 1 to (medianArray.length/2) - 1. This adjustment will ensure you access the correct index in the sorted medianArray.
    Once you make this change, give your code another try and see if the median calculation comes out as expected. Remember, programming is all about learning and improving over time, so don't worry if you encountered a small hiccup.
    By the way, if you ever need to roll a d12 or any other dice for testing or fun, you can use the Dice Roller tool at https://flipsimu.com/dice-roller/roll-d12/. It's a handy resource for generating random dice rolls and adding some excitement to your projects.
    Last edited by JasonCurtis; June 6th, 2023 at 08:55 AM.

  10. #10
    Junior Member azirafaelsish's Avatar
    Join Date
    Jan 2024
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculate median dice game HELP!

    It sounds like you're working on an interesting Java program. Calculating the median of dice rolls can be a bit tricky, but I'm sure we can figure it out.
    To help you better, it would be great if you could share some of your code or let us know specifically where you're getting stuck. That way, we can provide more targeted assistance.
    By the way, if you're into programming and looking to make some extra money, you might want to explore Apps That Pay You Real Money. It's a handy resource for finding apps that offer opportunities to earn while you pursue your coding interests.
    Last edited by azirafaelsish; January 15th, 2024 at 12:31 PM.

Similar Threads

  1. Dice Game! PLEASE HELP!
    By mhaxton in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2013, 12:47 PM
  2. dice game problem
    By Nick87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 23rd, 2013, 04:02 PM
  3. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  4. [SOLVED] Dice game need help
    By lf2killer in forum Loops & Control Statements
    Replies: 6
    Last Post: October 5th, 2012, 02:25 AM
  5. Replies: 2
    Last Post: August 30th, 2012, 03:26 AM