Question

In: Computer Science

Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the...

Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the user to guess four numbers.  Compare each of the user’s guesses to the four random numbers and display a message that includes the user’s guess, the randomly determined four-digit number, and the amount of points the user has won as follows:

No matches 0 points
Any one digit matching 5 points
Any two digits matching 100 points
Any three digits matching 2,000 points
All four digits matching 1,000,000 points

Solutions

Expert Solution

import java.text.NumberFormat;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

public class Lottery {

    /*

    Method to check whether x is present in values array.

    */

    public static boolean contains_number(int[] values, int x)

    {

        for(int num: values)

        {

            if(num == x)

            {

                return true;

            }

        }

        return false;

    }

   

    public static void main(String[] args)

    {

        int correct_guesses = 0;

        int total_score = 0;

        int[] random_nums = new int[4];

        int[] guesses = new int[4];

        // Asking user to guess four numbers

        System.out.println("------------------------------------------");

        System.out.println("\t\tLOTTERY GAME\t\t");

        System.out.println("------------------------------------------");

        System.out.println("Guess the four numbers between 0 and 9: ");

        for(int i=0; i<4; i++)

        {

            Scanner sc = new Scanner(System.in);

            guesses[i] = sc.nextInt();

        }

        ArrayList numbers = new ArrayList();

        for(int i=0; i<=9; i++)

        {

            numbers.add(i);

        }

        // Shuffling the arraylist containing values to create random numbers

        Collections.shuffle(numbers);

        // Taking out first 4 numbers randomly

        for(int i=0; i<4; i++)

        {

            int random_num = (int) numbers.get(i);

            random_nums[i] = random_num;

            // Checking whether one of the guessed number matches with random number.

            // if matches, increase the correct guess count

            if(contains_number(guesses, random_num))

            {

                correct_guesses++;

            }

        }

        // Calculating the user score

        switch(correct_guesses)

        {

            case 0:

                total_score = 0;

                break;

            case 1:

                total_score = 5;

                break;

            case 2:

                total_score = 100;

                break;

            case 3:

                total_score = 2000;

                break;

            case 4:

                total_score = 1000000;

                break;

            default:

                break;

        }

        // Dispalying the user guessed and randomly generated numbers

        System.out.print("The user guessed numbers are: ");

        for(int num: guesses)

        {

            System.out.print(String.valueOf(num) + " ");

        }

        System.out.println();

        System.out.print("The randomly generated numbers are: ");

        for(int num: random_nums)

        {

            System.out.print(String.valueOf(num) + " ");

        }

        System.out.println();

        // Printing total score of the user

        System.out.println("Total score is " + NumberFormat.getIntegerInstance().format(total_score) + " points.");

    }

   

}

OUTPUT


Related Solutions

In the game Super Vegas Lottery, four digits are drawn at random one at a time with replacement from 0 to 9.
1) In the game Super Vegas Lottery, four digits are drawn at random one at atime with replacement from 0 to 9.In other words, there are 10 slips of paper in a jar, each with a di erent digit printedon it. A slip is drawn from the jar, the number written down, then the slip is put backinto the jar, and the jar is shaken up. This process is repeated three more times.You win if any permutation of your numbers...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them to a text file on separate lines. Then the program should read the numbers from the file, calculate the average of the numbers, and display this to the screen.
One of the games of the Massachusetts Lottery is the daily numbers.  Four digits (0 - 9)...
One of the games of the Massachusetts Lottery is the daily numbers.  Four digits (0 - 9) are drawn at random thus producing a 4 digit number. Your friend has the following theories about the digits drawn: #1. Over the year the average of the total of the 4 digits drawn each day is 24 #2. If the first digit of the 4 digits drawn is even the last digit is odd at least 75% of the time #3. The middle...
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an...
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an ArrayList. Then search for the first instance of the number 3, print the position, and then remove it from the list.
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Write a C++ console application to simulate a guessing game. Generate a random integer between one...
Write a C++ console application to simulate a guessing game. Generate a random integer between one and 100 inclusive. Ask the user to guess the number. If the user’s number is lower than the random number, let the user know. If the number is higher, indicate that to the user. Prompt the user to enter another number. The game will continue until the user can find out what the random number is. Once the number is guessed correctly, display a...
Create a class called RandomGuess. In this game, generate and store a random number between 1...
Create a class called RandomGuess. In this game, generate and store a random number between 1 and 100. Then, continuously (in a loop): Ask the user to enter a number between 1 and 100 Let the user know if the guess is high or low, until the user enters the correct value If the user enters the correct value, then display a count of the number of attempts it took and exit
In a lottery, each ticket has 5 one-digit numbers 0-9 on it. (with no digit repeating...
In a lottery, each ticket has 5 one-digit numbers 0-9 on it. (with no digit repeating twice) You win only if your ticket has the digits in the required order. What are your chances of winning?
(1a) In a new lottery game called "Minibucks version 2", you select four numbers between 1...
(1a) In a new lottery game called "Minibucks version 2", you select four numbers between 1 and 25. If you match exactly three of your numbers with the four numbers chosen by the lottery that day, you win $5. What is the probability that you win this prize? Write your answer as a decimal accurate to four decimal places. (1b) A drawer contains 19 white socks and 5 black socks. Two different socks are selected from the drawer at random....
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT