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 3 of 3

Thread: A few tips in the direction of a lottery game, factorials

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A few tips in the direction of a lottery game, factorials

    This is an assignment for class that I am working on. I am not looking for answers, just a bit of guidance. I am stuck in a few places and have a feeling that I am really lacking. Anything at all is appreciated.

    I am mostly struggling with the jackpotChance method, have been instructed to use this formula: (n * ( n*1) * ( n*2 ) * . . . * ( n-k+1) divided by k * ( k-1) * ( k-2) * . . . *3 * 2 * 1) * m
    I have been attempting to write code for a factorial and am getting nowhere.

    A simulation of a lottery drawing. Player picks a set of numbers. A random drawing of numbers is then made, and the player wins if his/her chosen numbers match the drawn numbers (disregarding the order of the numbers).

    A player picks k distinct numbers between 1 and n (inclusive), as well as one bonus number between 1 and m (inclusive). “Distinct” means that none of the first k numbers may be repeated. However, the bonus number may or may not coincide with one of the k distinct numbers. The distinct numbers as well as the bonus number must match the drawn values for the player to win the jackpot.

    For example, in the TN Lottery’s Powerball game, a player picks five numbers between 1 and 59, inclusive, and one “Powerball number” between 1 and 35, inclusive. Here, the number of possible lottery tickets is:

    (59*58*57*56*55 / 5*4*3*2*1) * 35 = 175,223,510

    A single ticket’s chance of winning the jackpot is then 1/175,223,510, which is about 0.0000000057.

    (n * ( n*1) * ( n*2 ) * . . . * ( n-k+1) divided by k * ( k-1) * ( k-2) * . . . *3 * 2 * 1) * m

    Allow the user to enter values for k, n, and m above. Then, ask the user to enter the numbers that s/he wishes to play (this is your program’s equivalent of “buying” a lottery ticket). Store the k distinct numbers into an array, but keep the bonus number separate.

    The program should compute and display the probability of winning the jackpot for the selected game. Then, it should randomly “draw” the winning numbers and determine if the user-entered numbers match. Keep in mind that the matching for the k distinct values should disregard the order of the numbers. The winning numbers should be shown, along with an appropriate message indicating whether or not the user won the jackpot. Allow the user to play as many games as s/he wants.

    Error Checking Implement error checking on all user inputs, to the extent we’ve discussed it in class (don’t worry about data type mismatches). Error checking should repeatedly prompt for the input until the user enters a valid value. For example, the user should not be able to enter negative inputs. The user input should also be checked to see that it matches the entered values for n and m – for example, in a game where n = 59, the user should not be able to pick 60 for his/her ticket. Also make sure that you’re checking for distinct inputs when the player chooses numbers – a player shouldn’t be able to pick the same number more than once (with the exception of the bonus number).

    Code Organization Your program should use methods to break the code down into manageable chunks. At minimum, include the following methods (although you are welcome to add more if you want).


    import java.util.*;
     
    public class Lottery {
     
        //Returns the probability of winning the jackpot in a lottery drawing with the 
        //specified parameters.
     
        //STILL NEEDED: ALL
        public static double jackpotChance(int k, int n, int m) 
        {
            double jackpotChance = 0;
            System.out.println(factorial(n));
     
            return jackpotChance;
     
        }
     
        public static double factorial(int n) {
            double result = 1;
            for (int i = 1; i <= n; i++) {
                result *= i;
            }
            return result;
        }
     
        //Allows the user to enter k distinct integers between 1 and n.  Must include
        //error checking to ensure that each entered number is within the valid range and 
        //does not coincide with any previously entered number. Returns an array of
        //the entered numbers.
     
        //STILL NEEDED: ERROR CHECKING TO ENSURE USERPICK IS A DISTINCT NUMBER
        public static int[] enterNumbers(int k, int n)
        {
            System.out.println();
     
            Scanner input = new Scanner(System.in);
     
            //asks for user input, takes user input for variables k & n
            System.out.println("Enter k.");
            k = input.nextInt();
            System.out.println("Enter n.");
            n = input.nextInt();
     
            final int SIZE = k; //stores k into final variable SIZE
            int enterNumbers[]= new int[SIZE];  //declares enterNumbers array
            int userPick;
     
            System.out.println("Enter " + k + " distinct integers between 1 and " + n + ".");
     
            int i=SIZE;
            for (i = 0; i < enterNumbers.length; i++) {
                System.out.println("Enter the next number to store:");
                userPick = input.nextInt();
     
                if (userPick >= 1 && userPick <= n) {
                    enterNumbers[i] = userPick;
                } else {
                    System.out.println("Invalid input, please try again.");
                    i--;
                }
            }
     
            System.out.println();
            System.out.println("Your chosen numbers are: ");
            for (i = 0; i < enterNumbers.length; i++) { 
                System.out.print(enterNumbers[i] + " ");
            }
     
            System.out.println();
            System.out.println("Enter m.");
            int m = input.nextInt();
     
            jackpotChance(k,n,m);
            drawNumbers(k,n);
            return enterNumbers;
        } 
     
        //Draws k distinct random integers between 1 and n. Returns an array of the drawn 
        // numbers.
     
        //STILL NEEDED: ERROR CHECKING TO ENSURE RANDOMNUMBER IS A DISTINCT NUMBER
        public static int[] drawNumbers(int k, int n)
        {
            int drawNumbers[]= new int[k];  //declares enterNumbers array
     
            int randomNumber;
     
            int i=k;
            for (i = 0; i < drawNumbers.length; i++) {
                randomNumber = (int) (Math.random()*n + 1);
                drawNumbers[i] = randomNumber;
            }
     
            System.out.println();
            System.out.println("Your drawn numbers are: ");
            for (i = 0; i < drawNumbers.length; i++) { 
                System.out.print(drawNumbers[i] + " ");
            }
     
            return drawNumbers;
        }
     
        //Returns whether the two arrays a and b contain the same elements, without 
        //regardtoorder. Forexample,ifa={1,4,3,2}andb={1,2,3,4}, 
        //this method should return true.
        //     public static boolean containSameElements(int[] enterNumbers, int[] drawNumbers)
        //     {
        // 
        //     }
     
        public static void main (String[] args) 
        {
     
            System.out.println("Welcome to THE LOTTERY GAME!");
            System.out.println("----------------------------");
            int k = 0;
            int n = 0;
            enterNumbers(k,n);
     
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: A few tips in the direction of a lottery game, factorials

    What's wrong with the factorial() method you've written?

    Ask specific questions that let us know what you need help with right now, one thing at a time. If you're getting errors or results you think are incorrect, then post those and let us know what you think is wrong and/or how the results should be different.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A few tips in the direction of a lottery game, factorials

    This is what I have written:

    public static double jackpotChance(int k, int n, int m)
    {
    double jackpotChance = 0;

    System.out.println(n);

    int factTop = 1;
    int i = 1;
    while (i <= n) {
    factTop=factTop*i;

    i++;
    System.out.println(factTop);
    }
    System.out.println(factTop);

    return jackpotChance;
    }

    I am printing factTop (the top of the formula's fraction) to see what the compiler is doing and the output I get is: 1 2 6 24 120 720 5040 40320 and so on, it goes all the way through the negative ints. I see that I want 120. I thought while i < 5 would do the trick but I'm sure I'm missing something obvious.

Similar Threads

  1. Replies: 10
    Last Post: April 21st, 2013, 09:28 AM
  2. logic quition on 2d game direction(airhockey)
    By hwoarang69 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 19th, 2013, 06:50 PM
  3. Lottery Game Problem
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2012, 03:38 AM
  4. Looking for tips
    By TimoElPrimo in forum Member Introductions
    Replies: 3
    Last Post: February 15th, 2011, 08:51 PM
  5. Direction,
    By Time in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 21st, 2010, 05:21 PM

Tags for this Thread