Answer to Question #258021 in Java | JSP | JSF for finno

Question #258021

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.


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



1
Expert's answer
2021-10-28T06:59:11-0400
import java.util.Random;
import java.util.Scanner;


public class App {


	public static void main(String[] args) {


		Scanner keyBoard = new Scanner(System.in);
		int size = 5;
		Random random = new Random();
		int[] user = new int[size];
		int[] lottery = new int[size];
		for (int i = 0; i < size; i++) {
			lottery[i] = random.nextInt(10);
		}


		System.out.println("Enter " + size + " elements: ");
		for (int i = 0; i < size; i++) {
			user[i] = -1;
			while (user[i] < 0) {
				user[i] = keyBoard.nextInt();
			}
		}


		int count = 0;
		for (int i = 0; i < size; i++) {
			if (user[i] == lottery[i]) {
				count++;
			}
		}


		System.out.println("\nLottery :");
		for (int i = 0; i < size; i++) {
			System.out.print(lottery[i] + " ");
		}


		System.out.println("\nUser :");
		for (int i = 0; i < size; i++) {
			System.out.print(user[i] + " ");
		}


		if (count == size) {
			System.out.println("\nYou are the grand prize winner.");
		} else {
			System.out.println("\nThere are " + count + " matching digits");
		}
		keyBoard.close();


	}
}

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