Answer to Question #257182 in Java | JSP | JSF for Flying Bird

Question #257182

Write a java program that simulates a lottery. The program should have an array of five integers named lottery and should generate a random number in the range of 0 through 9 for each element in the array.

The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match. For example, the following shows the lottery array and the user array with sample numbers stored in each. There are two matching digits (elements 2 and 4).

lottery array: 7 4 9 1 3

user array: 4 2 9 7 3


The program should display the random numbers stored in the lottery array and the number of matching digits. If all of the digits match, display a message proclaiming the user as a grand prize winner.


Input Validation: Do not accept a negative value for lottery number. Also keep track of array size.


1
Expert's answer
2021-10-27T00:26:20-0400
import java.util.Scanner;
import java.util.Random;  
public class Main
{
	public static void main(String[] args) {
	    Scanner in=new Scanner(System.in);
	    int size = 5;
        int max = 10;
        Random random = new Random();  
        int [] arr = new int [size];
        int [] lottery = new int [size];
        for(int i=0;i<size;i++)
            lottery[i]=random.nextInt(max)+1;
        System.out.println("Enter "+size+" elements: ");
        for(int i=0;i<size;i++)
            arr[i]=in.nextInt();
        int count=0;
        for(int i=0;i<size;i++)
        if(arr[i]==lottery[i])
        count++;
        System.out.println("Lottery :");
        for(int i=0;i<size;i++)
        System.out.println(lottery[i]+" ");


        System.out.println("User :");
        for(int i=0;i<size;i++)
        System.out.println(arr[i]+" ");


        if(count==size){
        System.out.println("Congratulations. You are the grand prize winner");
        }
        else{
        System.out.println("There are "+count+" matching digits") ;   
        }
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS