JAVA PROGRAMMING. LotteryApplication Write a program that simulates a lottery. T
ID: 3866105 • Letter: J
Question
JAVA PROGRAMMING.
LotteryApplication Write a 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.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
class lottery
{
public static void main(String args[])
{
int num = 5;
int[] user = new int[num];
int[] lotnum = new int[num];
int matchnumber;
generate_num(lotnum);
getdata(user);
matchnumber = compare(lotnum, user);
System.out.println(" Lottery Numbers: " + lotnum[0] + " " + lotnum[1] + " " + lotnum[2] + " " + lotnum[3] + " " + lotnum[4] + " ");
System.out.println(" User Numbers:" + user[0] + " " + user[1] + " " + user[2] + " " + user[3] + " " + user[4] + " ");
System.out.println(" Number of Matching Digits: " +matchnumber);
if(matchnumber == 5)
System.out.println("Grand Prize Winner");
if(matchnumber == 4)
System.out.println(" Four Digits are matched");
if(matchnumber == 3)
System.out.println(" Three Digits are matched");
if(matchnumber == 2)
System.out.println(" Two Digits are matched");
if(matchnumber == 1)
System.out.println(" One Digit is matched:");
if(matchnumber == 0)
System.out.println(" No matching numbers:");
}
public static int generate_num(int[] lotnum)
{
Random rand = new Random();
lotnum[0] = rand.nextInt(10);
lotnum[1] = rand.nextInt(10);
lotnum[2] = rand.nextInt(10);
lotnum[3] = rand.nextInt(10);
lotnum[4] = rand.nextInt(10);
return lotnum[4];
}
public static int getdata(int[] user)
{
Scanner scan = new Scanner(System.in);
System.out.println(" Enter First digit: ");
user[0] = scan.nextInt();
System.out.println(" Enter Second Digit: ");
user[1] = scan.nextInt();
System.out.println(" Enter Third Digit: ");
user[2] = scan.nextInt();
System.out.println(" Enter Four Digit: ");
user[3] = scan.nextInt();
System.out.println(" Enter Five Digit: ");
user[4] = scan.nextInt();
scan.close();
return user[4];
}
public static int compare(int[] user, int[] lotnum)
{
int matchnumber = 0;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
if(lotnum[i] == user[j])
{
matchnumber++;
}
}
}
return matchnumber;
}
}
OUTPUT
Enter First digit: 3
Enter Second Digit: 5
Enter Third Digit: 4
Enter Four Digit: 6
Enter Five Digit: 8
Lottery Numbers: 1 3 2 0 8
User Numbers:3 5 4 6 8
Number of Matching Digits: 2
Two Digits are matched
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.