➤ How to Code a Game

➤ Array Programs in Java

➤ Java Inline Thread Creation

➤ Java Custom Exception

➤ Hibernate vs JDBC

➤ Object Relational Mapping

➤ Check Oracle DB Size

➤ Check Oracle DB Version

➤ Generation of Computers

➤ XML Pros & Cons

➤ Git Analytics & Its Uses

➤ Top Skills for Cloud Professional

➤ How to Hire Best Candidates

➤ Scrum Master Roles & Work

➤ CyberSecurity in Python

➤ Protect from Cyber-Attack

➤ Solve App Development Challenges

➤ Top Chrome Extensions for Twitch Users

➤ Mistakes That Can Ruin Your Test Metric Program

**Java Lottery Program Array** | In this section we will write a Java program for the lottery game. This is a game where the player has to draw the numbers randomly for the sake of the prize, this completely depends on luck, the player who gets the lottery numbers wins the prize.

For example:-

1) Enter 5 numbers: 1,6,8,16,34

The lottery numbers are: 45,23,33,12,2

Sorry you lost.

2) Enter 5 numbers: 45,23,33,12,2

The lottery numbers are: 45,23,33,12,2

Congrajulations, you won.

## Java Lottery Program Array Code

Now let us see the Java Lottery Program Array. We will use ArrayList instead of a simple Java array. And to generate the numbers we will use the Java Random class.

**Java Lottery Program Array Code**

```
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// generate winning numbers
List<Integer> winningNum = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 6; i++) {
while (true) {
// get a random number between 1 to 49
int winning = random.nextInt(49) + 1;
// add it to list if it is not exist
if (!winningNum.contains(winning)) {
winningNum.add(winning);
break;
}
}
}
// take input from user
Scanner scan = new Scanner(System.in);
System.out.println("================ Java Lottery Program Array " +
"================");
System.out.println("Please enter the 6 numbers you want to " +
"choose between 1 and 49");
List<Integer> guessedNum = new ArrayList<>();
for (int i = 0; i < 6; i++) {
System.out.println("The numbers you have chosen are: "
+ guessedNum);
System.out.print("Enter a number between 1-49: ");
while (true) {
try {
String numberString = scan.nextLine();
int number = Integer.parseInt(numberString);
if (number >= 1 && number <= 49) {
guessedNum.add(number);
break;
} else {
System.out.println(number +
" is not between 1 and 49. Please try again.");
}
} catch (NumberFormatException nfe) {
System.out.println("Hey, that's not even a number. "+
"Please try again.");
}
}
}
System.out.println("\nThe winning numbers were: " + winningNum);
System.out.println("Your numbers are: " + guessedNum);
guessedNum.retainAll(winningNum);
System.out.println("Your matched numbers are: " + guessedNum);
if (guessedNum.containsAll(winningNum)) {
System.out.println("Holy crap, you actually won. "+
"How did you do that?");
} else {
System.out.println("Sorry, you lost. Not surprising.");
}
scan.close();
}
}
```

Output:-

================ Java Lottery Program Array ================

Please enter the 6 numbers you want to choose between 1 and 49

The numbers you have chosen are: [ ]

Enter a number between 1-49: 15

The numbers you have chosen are: [15]

Enter a number between 1-49: 12

The numbers you have chosen are: [15, 12]

Enter a number between 1-49: 48

The numbers you have chosen are: [15, 12, 48]

Enter a number between 1-49: 42

The numbers you have chosen are: [15, 12, 48, 42]

Enter a number between 1-49: 45

The numbers you have chosen are: [15, 12, 48, 42, 45]

Enter a number between 1-49: 23

The winning numbers were: [40, 38, 24, 23, 18, 7]

Your numbers are: [15, 12, 48, 42, 45, 23]

Your matched numbers are: [23]

Sorry, you lost. Not surprising.

================ Java Lottery Program Array ================

Please enter the 6 numbers you want to choose between 1 and 49

The numbers you have chosen are: [ ]

Enter a number between 1-49: 10

The numbers you have chosen are: [10]

Enter a number between 1-49: 80

80 is not between 1 and 49. Please try again.

20

The numbers you have chosen are: [10, 20]

Enter a number between 1-49: KnowProgram

Hey, that’s not even a number. Please try again.

50

50 is not between 1 and 49. Please try again.

22

The numbers you have chosen are: [10, 20, 22]

Enter a number between 1-49: 25

The numbers you have chosen are: [10, 20, 22, 25]

Enter a number between 1-49: 26

The numbers you have chosen are: [10, 20, 22, 25, 26]

Enter a number between 1-49: 27

The winning numbers were: [41, 27, 9, 47, 36, 15]

Your numbers are: [10, 20, 22, 25, 26, 27]

Your matched numbers are: [27]

Sorry, you lost. Not surprising.

In the above Java Lottery Program Array, first, we have initialized the 6 random numbers using the Java Random class. After that, we have taken 6 valid numbers between 1 to 49 as input from the end-user.

Now, both the input list and winning list are compared. If both are exactly the same then the user wins the lottery game, else the user loses the game. Also see:- Hidden Word Java Program

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!