Repository navigation
Expand file tree
/
Copy pathlottery simulation.java
More file actions
98 lines (84 loc) · 1.86 KB
/
Copy pathlottery simulation.java
File metadata and controls
98 lines (84 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package week04;
import java.util.Random;
import java.util.Scanner;
public class temp5
{
public static void main(String[] args)
{
int[] my_numbers = {1,2, 3, 4, 5};//TakeNumbersFromUser(5);
Print(my_numbers);
int[] lottery_numbers = GenerateRandomNumbers(5);
Print(lottery_numbers);
int matches = PlayLottery(my_numbers, lottery_numbers);
System.out.println("You have "+matches+" matche(s)");
}
public static int PlayLottery(int[] my, int[] loto)
{
int counter = 0;
for (int i = 0; i < loto.length; i++) {
for (int j = 0; j < loto.length; j++) {
if(my[i] == loto[j])
counter++;
}
}
return counter;
}
public static void Print(int[] a)
{
//print the numbers in the array
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println();
}
public static int[] TakeNumbersFromUser(int n)
{
int[] a = new int[n];
//fill this with user numbers:
Scanner sc = new Scanner(System.in);
for (int i = 0; i < n; i++)
{
System.out.println("Enter the next number between [0, 50]:");
int number = sc.nextInt(); //i=0 -> 50, i=1 -> 50, i=2
if(Exists(number, a))
{
System.out.println("The number already exists. Enter a new number. ");
i--;
continue;
}
if(number < 0 || number > 50)
{
System.out.println("Outside bounds. Try again");
i--;
continue;
}
a[i] = number;
}
return a;
}
public static boolean Exists(int number, int[] a)
{
for (int i = 0; i < a.length; i++) {
if(a[i] == number)
return true;
}
return false;
}
public static int[] GenerateRandomNumbers(int n)
{
int[] a = new int[n];
//fill this with random numbers
Random r = new Random();
for (int i = 0; i < n; i++)
{
int number = r.nextInt(51);
if(Exists(number, a))
{
i--;
continue;
}
a[i] = number;
}
return a;
}
}