Our professor tasked us to create a lotto program that will generate 6 random numbers ranging from 1-55 without us using the arrays and loops, we can only use "if else" statements. We also have to put scanner in which the user/better will input his 6 numbers also ranging from 1-55. The user's numbers should win as long as the 6 of the numbers match regardless of arrangements. For example
User: 1,2,3,4,5,6
Random Number:6,3,4,2,1,5
System: You Win!
I created a lotto program but my program requires every numbers to match with regards to their arrangement
User: 1,2,3,4,5,6
Random:6,5,4,3,2,1
System: You Lose
I don't have any idea how to ignore the arrangements. I tried search for a lot of thing in the internet and they mostly use arrays and loops.
CodePudding user response:
public class LottoGame {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
System.out.print("Input 6 numbers [1-55] separated with space: ");
NumberSet userNumberSet = new NumberSet();
userNumberSet.a = new AtomicInteger(scan.nextInt());
userNumberSet.b = new AtomicInteger(scan.nextInt());
userNumberSet.c = new AtomicInteger(scan.nextInt());
userNumberSet.d = new AtomicInteger(scan.nextInt());
userNumberSet.e = new AtomicInteger(scan.nextInt());
userNumberSet.f = new AtomicInteger(scan.nextInt());
System.out.format("User: %d,%d,%d,%d,%d,%d\n", userNumberSet.a.get(), userNumberSet.b.get(),
userNumberSet.c.get(), userNumberSet.d.get(), userNumberSet.e.get(), userNumberSet.f.get());
userNumberSet.sort();
NumberSet randomNumberSet = getRandomNumberSet();
System.out.format("Randome Number: %d,%d,%d,%d,%d,%d\n", randomNumberSet.a.get(), randomNumberSet.b.get(),
randomNumberSet.c.get(), randomNumberSet.d.get(), randomNumberSet.e.get(),
randomNumberSet.f.get());
randomNumberSet.sort();
boolean win = userNumberSet.a.get() == randomNumberSet.a.get();
win &= userNumberSet.b.get() == randomNumberSet.b.get();
win &= userNumberSet.c.get() == randomNumberSet.c.get();
win &= userNumberSet.d.get() == randomNumberSet.d.get();
win &= userNumberSet.e.get() == randomNumberSet.e.get();
win &= userNumberSet.f.get() == randomNumberSet.f.get();
System.out.println("System: " (win ? "You Win!" : "You Lose"));
}
private static NumberSet getRandomNumberSet() {
Random random = new Random();
NumberSet numberSet = new NumberSet();
numberSet.a = new AtomicInteger(random.nextInt(55) 1);
numberSet.b = new AtomicInteger(random.nextInt(55) 1);
numberSet.c = new AtomicInteger(random.nextInt(55) 1);
numberSet.d = new AtomicInteger(random.nextInt(55) 1);
numberSet.e = new AtomicInteger(random.nextInt(55) 1);
numberSet.f = new AtomicInteger(random.nextInt(55) 1);
return numberSet;
}
private static final class NumberSet {
private AtomicInteger a;
private AtomicInteger b;
private AtomicInteger c;
private AtomicInteger d;
private AtomicInteger e;
private AtomicInteger f;
public void sort() {
sort(a, b);
sort(b, c);
sort(c, d);
sort(d, e);
sort(e, f);
}
private void sort(AtomicInteger one, AtomicInteger two) {
if (one.get() > two.get()) {
int tmp = one.get();
one.set(two.get());
two.set(tmp);
}
if (two == b) {
sort(one, c);
} else if (two == c) {
sort(one, d);
} else if (two == d) {
sort(one, e);
} else if (two == e) {
sort(one, f);
}
}
}
}
P.S. I think you are able to update a bit this snippte to check that NumberSet
contains only unique numbers.
CodePudding user response:
It's not that efficient and not that clean as code should be, but if you can only use if (else) statements and no loops, you can write a method like the following and call that method for each number your user has typed:
public static boolean LottoNumbersContains(int number,
int lottoNumber1,
int lottoNumber2,
int lottoNumber3,
int lottoNumber4,
int lottoNumber5,
int lottoNumber6)
{
return number == lottoNumber1 || number == lottoNumber2 || number == lottoNumber3 || number == lottoNumber4 ||
number == lottoNumber5 || number == lottoNumber6;
}
The method returns whether the given number equals to any of the given lottoNumbers by checking each lottoNumber if it equals the given number. If you are not allowed to use "||", you can also write it out with if-statements:
if(number == lottoNumber1){
return true;
}
if(number == lottoNumber2){
return true;
}
//{...}
return false;
*Note that naming conventions are not followed. Do not copy the code to your work, the code is just an example to orient yourself.