Write a java program that simulates a lottery Your program s
Write a java program that simulates a lottery. Your program should have an array of five integers named lotteryNumbers. Use the Random class to generate a random number in the range of 0 through 9 for each element in the array. Your program should either read in five integers that represent a person’s lottery picks or let the computer to select numbers for the user. The program should then display the number of digits that match the lottery numbers.
Write the following methods in the program:
public static int[] generateLotteryNumber () – This method should use the Random class to generate a random number in the range of 0 through 9 for each element in the array. Return the array of randomly generated numbers.
public static int compareNumbers (int[] lottery, int[] picks) – This method is to compare the corresponding elements in the two arrays and return the number of digits that match.
Solution
package chegg;
import java.util.Random;
import java.util.Scanner;
public class Lottery {
public static int[] generateLotteryNumber()
{
/* array of five integers named lotteryNumbers*/
int []lotteryNumbers=new int[5];
/* Random is java method that use for generate random number*/
Random rand = new Random();
/* random number generation method is rand.nextInt((max - min) + 1) + min;
* here max is maximum value and min is minimum value
*/
for(int i=0;i<5;i++)
{ /* generate five random number between 0 to 9 and put in lotteryNumbers array*/
lotteryNumbers[i]=rand.nextInt((9 - 0) + 1) + 0;
System.out.println(\"lotteryNumbers[i] is:\"+lotteryNumbers[i]);
}
/* retun lotteryNumbers array method*/
return lotteryNumbers;
}
/*This method is to compare the corresponding elements in the two arrays and
* return the number of digits that match.*/
public static int compareNumbers(int[] lottery, int[] picks)
{
/* initially define integer variable match with value 10*/
int match = 10;
int i;
/*using for loop compare the corresponding elements in the two arrays
* here compare first lottery number to picks one,
* compare Second lottery number to picks second,
* compare third lottery number to picks third,
* compare fourth lottery number to picks fourth,
* compare five lottery number to picks five*/
for(i=0;i<5;i++)
{
if(lottery[i]==picks[i])
{
/*if lottery number[i] is equal to picks[i] then return it*/
match=picks[i];
return match;
}
}
return match;
}
public static void main(String[] args)
{
// System.out.println(\"lottery number is\"+ lotteryNumbers[]);
Scanner sc=new Scanner(System.in);
int []picks=new int[5];
int i;
int matchNumber;
System.out.println(\"Enter number for lottery picks between 0 to 9 \");
for(i=0;i<5;i++)
{
picks[i]=sc.nextInt();
}
/*it call generateLotteryNumber method*/
int []lotteryNumbers=generateLotteryNumber();
/*it call compareNumbers method*/
matchNumber=compareNumbers(lotteryNumbers,picks);
if(matchNumber==10)
{
System.out.println(\"Sorry, No number matach from Lottery number!! please Try Again\");
}
else
{
System.out.println(\"The number of digits that match is : \"+ matchNumber);
}
}
}
--------------------------------------------------------------------------------------------------
output sample1 :-
Enter number for lottery picks between 0 to 9
1
2
3
4
5
lotteryNumbers[i] is:1
lotteryNumbers[i] is:9
lotteryNumbers[i] is:6
lotteryNumbers[i] is:4
lotteryNumbers[i] is:4
The number of digits that match is : 1
--------------------
output sample 2:-
Enter number for lottery picks between 0 to 9
3
4
5
6
7
lotteryNumbers[i] is:1
lotteryNumbers[i] is:2
lotteryNumbers[i] is:5
lotteryNumbers[i] is:3
lotteryNumbers[i] is:7
The number of digits that match is : 5
----------------------------
output sample 3:-
Enter number for lottery picks between 0 to 9
6
7
4
3
2
lotteryNumbers[i] is:7
lotteryNumbers[i] is:2
lotteryNumbers[i] is:7
lotteryNumbers[i] is:7
lotteryNumbers[i] is:5
Sorry, No number matach from Lottery number!! please Try Again
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.


