简体   繁体   中英

Optimizing If- else if- else statements in Java

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?

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);

        }

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)) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM