LuckyNumbers is ok so far, but it gives numbers [0..9]
, so + 1
:;
class Test {
public int luckyNumbers() {
return (int) (Math.random() * 10) + 1;
}
..and in the main method typically (with System.out.println()
):
public static void main(String... args) {
Test testObj = new Test();
for (int i = 1; i <= 3; i++) {
System.out.println(testObj.luckyNumbers());
}
}
}
EDIT:
Voila (not in main method, but then (why not!?) void
):
public void luckyNumbers() {
for (int i = 1; i <= 3; i++) {
System.out.println((int) (Math.random() * 10) + 1);
}
}
public static void main(String... args) {
new Test().luckyNumbers();
}
EDIT2:
Voila, 3 numbers [1-10]:
public int[] luckyNumbers() {
return new int[] {
Math.random() * 10) + 1,
Math.random() * 10) + 1,
Math.random() * 10) + 1
};
}
EDIT3 (drawLuckyUsers method):
- Input: a List of all users, with individual lucky number [1-10]
- Output: a List of winning users
public java.util.List<User> drawLuckyUsers(java.util.List<User> allUsers) {
//helper structure to map all users by luckyNumber
java.util.Map<Integer, List<User>> helper = new java.util.TreeMap<>();
for (User u : allUsers) {
if(helper.conatins(u.getLuckyNumber())) {
helper.get(u.getLuckyNumber()).add(u);
} else {
List<User> tmp = new ArrayList<>(2);
tmp.add(u);
helper.put(u.getLuckyNumber(), tmp);
}
}
java.util.List<User> winner = new java.util.ArrayList<>();
for(int i = 0; i < 3; i++) {
winner.addAll(helper.get(luckyNumbers()));
}
return winner;
}
with.. luckNumbers() v1:
public int luckyNumbers() {
return (int) (Math.random() * 10) + 1;
}