447,326 views
20 votes
20 votes
Describe the necessary Java commands to create a Java program for creating a lottery program using arrays and methods.

If the user wants to purchase 5 lottery tickets, which looping structure would you use, and why?

User Jeff Putz
by
3.2k points

2 Answers

13 votes
13 votes
I think each lottery ticket will cost £5 each so you would count in 5s till you reach the amount of tickets to get the answer which is £25
User Vu Phung
by
3.6k points
14 votes
14 votes

Answer: would use the for loop in this java program. A for loop is a repetition control structure that allows you to write a loop that needs to be executed a specific number of times which is why it's beneficial for the lottery program. A for loop is useful when you know how many times a task is to be repeated. import java.util.Random;import java.util.Scanner;public class LotteryGame

{ public static void main(String[] args) { // TODO Auto-generated method stub

Random r = new Random();
int random[] = new int[5];

for (int i = 0; i < 5; ++i) {
random[i] = r.nextInt(48) + 1; }

Scanner


sc = new Scanner(System.in);
int usernumbers[] = new int[5];

System.out.print("Enter 5 numbers: ");

for (int i = 0; i < 5; ++i) {

usernumbers[i] = sc.nextInt(); }

System.out.print("\\Random Numbers: ");
for (int i = 0; i < 5; ++i)

System.out.print(random[i] + " ");
System.out.print("\\User Entered Numbers: "); for (int i = 0; i < 5; ++i)

System.out.print(usernumbers[i] + " ");

int match = 0;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {

if (random[i] == usernumbers[j])
++match; }

}
System.out.println("\\There are " + match + " number of matches"); }

Step-by-step explanation:

User SamuelMS
by
3.6k points