I\'m working on a lottery application taht simulates a lottery. The class should
ID: 3534064 • Letter: I
Question
I'm working on a lottery application taht simulates a lottery. The class should have an array of five integers named lotteryNumbers. The constructor should use the Random class to generate a random number in the range of 0 through 9 for each element in the array. The class should also have a method that accepts an array of five integers taht represent a person's lottery pick. The method ids to compare the corresponding elements in the two arrays and return the number of digits that match. For example, the following shows the lotteryNumbers array and the user's array with sample numbers stored in each. There are two matching digits (elements 2 and 4).
lottery Numbers array:
7 4 9 1 3
User's array:
4 2 9 7 3
0 matches = sorry no prize
1 match = $2
2 matches = $5
3 matches = $10
4 matches = $5,000
5 matches = $50,000
Here is my code. It works but doesn't give me the correct output I want.
import java.util.Random;
import java.util.Scanner;
class Lottery {
/**
* The lottery numbers.
*/
private int lotteryNumbers[];
/**
* Default Constructor.
*
* The class should use the Random class (from the Java API) to generate a
* random number in the range of 0 through 9 for each element in the array.
*/
public Lottery() {
Random rand = new Random(System.currentTimeMillis());
lotteryNumbers = new int[5];
for (int i = 0; i < lotteryNumbers.length; i++) {
lotteryNumbers[i] = Math.abs(rand.nextInt()) % 10;
}
}
/**
* The class should also have a method that accepts an array of 5 integers
* that represent a person's lottery picks. The method is to compare the
* corresponding elements in the two arrays and return the number of digits
* that match.
*/
public int compareNumbers(int[] usersNumbers) {
int match = 0;
if (usersNumbers.length == lotteryNumbers.length) {
for (int i = 0; i < lotteryNumbers.length; i++) {
if (usersNumbers[i] == lotteryNumbers[i]) {
match++;
}
}
}
return match;
}
/**
* In addition, the class should have a method that returns a copy of the
* lotteryNumbers array.
*/
public int[] getLotteryNumbers() {
return lotteryNumbers;
}
}
public class LotteryApplication {
public static void main(String[] args) {
Lottery lottery = new Lottery();
int lotteryNumbersCount = lottery.getLotteryNumbers().length;
System.out.println("Lottery Application ");
System.out.println("There are " + lotteryNumbersCount
+ " secret numbers in range of 0 through 9. "
+ "Try to guess them!!! ");
// Asks the user to enter five numbers.
Scanner kb = new Scanner(System.in);
int numbers[] = new int[lotteryNumbersCount];
for (int i = 0; i < numbers.length; i++) {
System.out.print(String.format("Enter Number %d: ", i+1));
numbers[i] = kb.nextInt();
}
// Display the number of digits that match the randomly generate
// lottery numbers.
int match = lottery.compareNumbers(numbers);
if (match == lotteryNumbersCount) {
// If all of the digits match, display a message proclaiming the
// user a grand prize winner.
System.out.println(" WOHOO! ALL CORRECT! YOU WON THE BIG PRIZE!" + match + "number(s).");
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
class Lottery {
/**
* The lottery numbers.
*/
private int lotteryNumbers[];
/**
* Default Constructor.
*
* The class should use the Random class (from the Java API) to generate a
* random number in the range of 0 through 9 for each element in the array.
*/
public Lottery() {
Random rand = new Random(System.currentTimeMillis());
lotteryNumbers = new int[5];
for (int i = 0; i < lotteryNumbers.length; i++) {
lotteryNumbers[i] = Math.abs(rand.nextInt()) % 10;
}
}
/**
* The class should also have a method that accepts an array of 5 integers
* that represent a person's lottery picks. The method is to compare the
* corresponding elements in the two arrays and return the number of digits
* that match.
*/
public int compareNumbers(int[] usersNumbers) {
int match = 0;
if (usersNumbers.length == lotteryNumbers.length) {
for (int i = 0; i < lotteryNumbers.length; i++) {
System.out.println(lotteryNumbers[i]);
if (usersNumbers[i] == lotteryNumbers[i]) {
match++;
}
}
}
return match;
}
/**
* In addition, the class should have a method that returns a copy of the
* lotteryNumbers array.
*/
public int[] getLotteryNumbers() {
return lotteryNumbers;
}
}
public class LotteryApplication {
public static void main(String[] args) {
Lottery lottery = new Lottery();
int lotteryNumbersCount = lottery.getLotteryNumbers().length;
System.out.println("Lottery Application ");
System.out.println("There are " + lotteryNumbersCount
+ " secret numbers in range of 0 through 9. "
+ "Try to guess them!!! ");
// Asks the user to enter five numbers.
Scanner kb = new Scanner(System.in);
int numbers[] = new int[lotteryNumbersCount];
for (int i = 0; i < numbers.length; i++) {
System.out.print(String.format("Enter Number %d: ", i+1));
numbers[i] = kb.nextInt();
}
// Display the number of digits that match the randomly generate
// lottery numbers.
int match = lottery.compareNumbers(numbers);
if(match == 0)
System.out.println(" sorry no prize");
else if(match == 1)
System.out.println(" $2 awarded");
else if(match == 2)
System.out.println(" $5 awarded");
else if(match == 3)
System.out.println(" $10 awarded");
else if(match == 4)
System.out.println(" $5000 awarded");
if (match == lotteryNumbersCount) {
// If all of the digits match, display a message proclaiming the
// user a grand prize winner.
System.out.println(" WOHOO! ALL CORRECT! YOU WON THE BIG PRIZE! $50000 awarded :" + match + "number(s).");
}
}
}