lundi 17 juillet 2017

I need help first of all in checking my code for the random generator part and then I need help finishing up my code for getuserLotteryPicks method

Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto").  The class should have an array of six integers named lotteryNumbers, and another array of six integers named userLotteryPicks.  The class' constructor should use the Random class to generate a unique random number in the range of 1 to 60 for each of the 6 elements in the lotteryNumbers array. Thus, there should be a loop that keeps generating random numbers until all 6 numbers are unique.  The Lottery class should have a method, getUsersPicks(), which prompts the user to enter 6 unique numbers between 1 - 60, which will be stored in the array of integers named usersLotteryPicks.  Thus, there should be validation to ensure that each number the user selects is different from all the other numbers that he/she entered previously, and to keep looping until the user enters 6 unique numbers.  Finally, the Lottery class should have a method, checkLotteryMatch(), which will compare the 2 arrays - lotteryNumbers & usersLotteryPicks - and return the number of digits that match.  The digits do not have to match in the exact order, so a nested loop should be created that goes through each digit in the user's lottery picks array and compares it to each digit in the lotteryNumbers array, counting each of the matches. The checkLotteryMatch() method will return an int containing the number of matches. Write a Driver class called the LotteryGame, which instantiates the Lottery class.  Once a Lottery object has been created, the driver will call the Lottery object's getUsersPicks() method, followed by the checkLotteryMatch() method.  Use the results of the checkLotteryMatch() method to determine if the user is a winner by using the following criteria: For a 3-digit match, display a message to the user that he/she will receive a free Lottery ticket as the prize For a 4-digit match, display a message to the user that he/she will receive a $2000 prize For a 5-digit match, display a message to the user that he/she will receive a prize of $50,000. For a 6-digit match, display a message to the user that he/she will receive a grand prize of $1,000,000. If there are no matches, display the following message to the user:  "Sorry, no matches today.  Try again!"

The code I have so far :

import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author nvale010
 */
public class Lottery {

    private int lotteryNumbers[] = new int[6];
    private int userLotteryPicks[] = new int[6];
    private boolean[] flag = new boolean[6];

    public Lottery() {

        Random myRan = new Random();
        int myRan1;

        for (int i = 0; i < 6; i++) {
            myRan1 = myRan.nextInt(60) + 1; // Random number created here.
            for (int x = 0; x < i; x++) {
                if (lotteryNumbers[x] == myRan1) // Here, code checks if same random number generated before.
                {
                    myRan1 = myRan.nextInt(60) + 1;// If random number is same, another number generated.
                    x = -1; // restart the loop
                }

            }
            lotteryNumbers[i] = myRan1;
        }

    }

    public int[] getLotteryNumbers() {
        return lotteryNumbers;
    }

    public void setLotteryNumbers(int[] lotteryNumbers) {
        this.lotteryNumbers = lotteryNumbers;
    }

    public int[] getUserLotteryPicks() {
        return userLotteryPicks;
    }

    public void setUserLotteryPicks(int[] userLotteryPicks) {
        this.userLotteryPicks = userLotteryPicks;
    }

    public void getUserPicks() {
        Scanner keyboard = new Scanner(System.in);

        for (int i = 0; i < userLotteryPicks.length; i++) {
            System.out.println("Enter a number between 1 and 60");
            userLotteryPicks[i] = keyboard.nextInt();
            flag[i] = false;
            for (int j = i + 1; j < userLotteryPicks.length; j++) {
                if (userLotteryPicks[i] == userLotteryPicks[j]) {
                    flag[i] = true;

                }
            }
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire