Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the Lottery program so that it asks the user to enter a series of 5 number

ID: 3825225 • Letter: W

Question

Write the Lottery program so that it asks the user to enter a series of 5 numbers in the range of 0 through 9 for each element in the array. After the user enters the numbers, the program compares them to 5 numbers that it randomly generated and prints out the how many numbers matched and the matching numbers. The program has to be written according to these rules: The user submitted numbers will be stored in an array. The randomly generated lottery numbers will be stored in an array. The two arrays will be compared to each other to find out the matching numbers. If the user matches all five numbers, the program should print that the user won the lottery. The program will be written in a modular way, meaning that: The user values will be received through a getValues method. The comparison will be also made by a compareNumbers method. This method will receive both the lottery numbers array and the user array and will return the matching numbers. You can have repeating numbers In the lottery. Sample output: Enter a series of 5 numbers. Enter number 1: 3 Enter number 2: 4 Enter number 3: 5 Enter number 4: 1 Enter number 5: 2 The lottery numbers are: 7 4 3 4 0 You matched 1 numbers The matching number are: 4

Explanation / Answer

package lottery;

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;
public class Lottery {

public static void main(String[] args) {
int[] numbers = new int[5];
int[] winningNumbers = new int[5]; //array holding 5 random numbers
int[] userNumber = new int[5]; //array holding the input
Scanner theNumbers = new Scanner(System.in);
int guesses;
int counter;
int i;

/*
for(i = 0; i < numbers.length; i++){
numbers[i] = i + 1;
out.println(numbers[i]);
}
*/

//generate 5 random numbers

for(i = 0; i < winningNumbers.length; i++ ){
int randomNums = new Random().next(49) + 1;
winningNumbers[i] = randomNums;   
}

out.println("Enter the 5 numbers");
for(i = 0; i < userNumber.length; i++){
guesses = theNumbers.nextInt();
userNumber[i] = guesses;
out.println(userNumber[i]);
if(winningNumbers[i] == userNumber[i] ){
counter+=1;
}
}
if (counter == 5){
out.println("you won!! COngratulations!");
else
out.println("you lose!");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote