-2

In doing a lotto checker program, where one of the constraints is to not use loops, or data structures like arrays, lists etc I've written the following piece of code to check if the 3 entries given by the user (draw1, draw2, draw3) are equal to any of the 7 random numbers generated by the program (random1, random2, random3, ...).

if (random1==draw1 || random2==draw1 || random3==draw1 || random4==draw1|| random5==draw1
    || random6==draw1 || random7==draw1)
    {
        if(random1==draw2 || random2==draw2 || random3==draw2 || random4==draw2|| random5==draw2
        || random6==draw2 || random7==draw2)
        {
            if(random1==draw3 || random2==draw3 || random3==draw3 || random4==draw3|| random5==draw3
            || random6==draw3 || random7==draw3)
            {
                str = str +'\n' + "The following 3 matches were found:" +'\n'+ draw1 + " " + draw2
                + " " + draw3 ;
            }else
            {
                str = str + '\n' + "The following 2 matches were found:" + '\n' + draw1 + " " + draw2;
            }
        }else if (random1==draw3 || random2==draw3 || random3==draw3 || random4==draw3|| random5==draw3
        || random6==draw3 || random7==draw3)
        {
            str = str + '\n' + "The following 2 matches were found:" + '\n' + draw1 + " " + draw3 ;
        }
        else
        {
            str = str + '\n' + "The following 1 matches were found:" + '\n' + draw1;
        }
    }else if (random1==draw2 || random2==draw2 || random3==draw2 || random4==draw2|| random5==draw2
    || random6==draw2 || random7==draw2)
    {
        if(random1==draw3 || random2==draw3 || random3==draw3 || random4==draw3|| random5==draw3
        || random6==draw3 || random7==draw3)
        {
            str = str + '\n' + "The following 2 matches were found:" + '\n' + draw2 + " " + draw3;
        }
        else
        {
            str = str + '\n' + "The following 1 matches were found:" + '\n' + draw2;
        }
    }
    else if (random1==draw3 || random2==draw3 || random3==draw3 || random4==draw3|| random5==draw3
    || random6==draw3 || random7==draw3)
    {
        str = str + '\n' + "The following 1 matches were found:" + '\n' + draw3;
    }
    else
    {
        str = str + '\n' + "The following 0 matches were found:" ;
    }

How can I go about optimizing this and most importantly will the optimization only increase readability or will it contribute to the efficiency of the program?

2 Answers2

0

Use sets, this will improve readabilty.

    Set<Integer> luckyNumbers = new HashSet<>();
    //Add your numbers to the set
    //Integer i = ...
    //luckyNumbers.add(i);

    Set<Integer> drawNumbers = new HashSet<>();
    //Integer i = ...
    //drawNumbers.add(i);

    Set<Integer> matchNumbers = new HashSet<>(luckyNumbers);
    matchNumbers.retainAll(drawNumbers);
    //In matchNumbers will be the intersection of the previous sets.
    //There you can get the size of the intersection set or its content to show the
    //matches

If you want to use a loop for this part you can do something like this:

System.out.println("Number of matches: " + matchNumbers.size());

        for(Integer matchNumber : matchNumbers){

            System.out.println("Match number: " + matchNumber);

        }
MDG
  • 41
  • 1
  • 4
  • We're not supposed to use loops! Also, I didn't mention it before (sorry!) but we're not allowed to use data structures like sets or lists either. – Justuraveragemathsstudent Aug 08 '20 at 11:27
  • 1
    honestly...tell your prof that this is just stupid and will not teach you anything that you will need at any point in your career. Except you want to become a prof ;) – muetzenflo Aug 08 '20 at 20:30
0

Try this.

static boolean equalsAny(int base, int... comparisons) {
    for (int c : comparisons)
        if (base == c)
            return true;
    return false;
}

and you can rewrite

 if (random1 == draw1 || random2 == draw1 || random3 == draw1 || random4 == draw1 || random5 == draw1
     || random6 == draw1 || random7 == draw1) {

to

if (equalsAny(draw1, random1, random2, random3, random4, random5, random6, random7)) {