简体   繁体   English

优化 Java 中的 If- else if- else 语句

[英]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, ... ).在做一个乐透检查程序时,其中一个约束是不使用循环,或者像 arrays、列表等数据结构,我编写了以下代码来检查用户给出的 3 个条目( draw1、draw2、 draw3 ) 等于程序生成的 7 个随机数中的任何一个 ( 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?我怎么能 go 关于优化这个,最重要的是优化只会增加可读性还是会有助于程序的效率?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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