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

C++ programming Create an application for generating lottery tickets of random numbers. Each lottery ticket has...
C++ programming Create an application for generating lottery tickets of random numbers. Each lottery ticket has a fixed count of numbers to be played with each number selected being within a given range. For example, when the range of numbers is 1 through 100, and 5 numbers are to be played, the lottery ticket will be composed of 5 unique values between 1 and 100. The values are selected at random. The main function is responsible for obtaining the upper...
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?
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT