【问题标题】:Optimizing If- else if- else statements in Java优化 Java 中的 If- else if- else 语句
【发布时间】:2023-03-28 03:38:01
【问题描述】:

在做乐透检查程序时,其中一个限制是不使用循环,或数组、列表等数据结构 我编写了以下代码来检查用户给出的 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:" ;
    }

我该如何进行优化?最重要的是,优化只会增加可读性还是会提高程序的效率?

【问题讨论】:

  • 你可以使用像ArrayList.contains(...)这样的原生方法吗?
  • 将随机数放入一个集合,将抽奖号码放入另一个集合,然后求交集的大小。 stackoverflow.com/questions/8882097/…
  • 抱歉,我现在编辑了这个问题。我们不允许使用任何数据结构,因此不能使用集合、数组或列表。
  • 如果你不使用循环或像Set 这样的数据结构,你将编写if-else 块的7^3=343 排列。我不认为你愿意这样做。
  • 为什么我必须编写 343 个 if-else 块的排列?我很困惑。

标签: java if-statement optimization logic


【解决方案1】:

使用集合,这将提高可读性。

    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

如果你想在这部分使用循环,你可以这样做:

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

        for(Integer matchNumber : matchNumbers){

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

        }

【讨论】:

  • 我们不应该使用循环!另外,我之前没有提到它(对不起!),但我们也不允许使用集合或列表等数据结构。
  • 老实说......告诉你的教授,这只是愚蠢的,不会教你任何你在职业生涯中任何时候需要的东西。除非你想成为一名教授;)
【解决方案2】:

试试这个。

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

你可以重写

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

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

【讨论】:

    猜你喜欢
    • 2020-11-23
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 2014-03-13
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多