【问题标题】:Java - Check for x alike integers in a list of integersJava - 在整数列表中检查 x 相似的整数
【发布时间】:2018-06-04 16:11:37
【问题描述】:

如果我找到 6 个相同的整数,或者如果有 5 个相同的整数,或者如果有 4 个相同的整数等等,我想查看一个整数列表并采取行动……我该怎么做.我有方法:

public int calculatePoints(ArrayList<Integer> dice) {
    //Check for 6 alike
    //if not, check for 5 alike
    //if not, check for 4 alike
}

【问题讨论】:

  • 你试过什么?你具体是在坚持什么?
  • 到目前为止你尝试了什么?
  • 我想不出解决问题的办法。 list-argument 可以有任何给定数量的元素,我不知道如何运行它来检查是否有 x 个相同的整数。
  • 我想添加一个 for 循环遍历参数列表中的所有元素: for(Integer num : dice) { } 可以创建一个 Map,看到这是为了检查有多少类似的 dice在列表中。 Map 是 Map

标签: java arrays for-loop if-statement integer


【解决方案1】:

这是一种方法:(假设您只想检查出现次数最多的数字

public static int calculatePoints(ArrayList<Integer> dice) {
        int max = 0;
        for(Integer die : dice) {
            int temp = Collections.frequency(dice, die);
            if(temp > max) {
                max = temp;
            }
        }
        if(max >= 6) {
            //Do stuff
        } else if (max >=5) {
            //Do stuff
        } else if (max >= 4) {
            //Do stuff
        }        
        return 0;
    }

基本逻辑:

-遍历数组中的每个Integer 并使用frequency 方法查看它发生了多少次。如果大于最大值,则将该数字解析为max。然后在最后,它if-else 语句检查数字出现了多少次,并在if 语句中执行您想要的操作。

【讨论】:

  • 感谢您的回复!请查看我发布的答案
【解决方案2】:

由于我没有足够的声誉来编辑我的帖子,这是我的答案@GBlodgett:

嘿!谢谢回复。我正在制作Farkle游戏,因此我需要找出列表中有多少个1s,2s,3s,4s,5s和6s,然后再给分(遵循规则)。我编写了一个似乎可以工作的代码,尽管必须有一种更有效的方法来做到这一点。请随时查看并给我一些反馈:

public int calculatePoints(ArrayList dice) {

    int points = 0;
    Map<Integer,Integer> numOfDice = new HashMap<Integer,Integer>();
    numOfDice.put(1, 0);
    numOfDice.put(2, 0);
    numOfDice.put(3, 0);
    numOfDice.put(4, 0);
    numOfDice.put(5, 0);
    numOfDice.put(6, 0);

    for(Integer num : dice) {
        if(numOfDice.get(num) == null) {
            numOfDice.put(num, 1);
        }
        else {
        numOfDice.put(num, numOfDice.get(num)+1);
        }
    }
    for(int i=1; i <= numOfDice.size(); i++) {
        if(i == 1) {
            if(numOfDice.get(1) < 3) {
                points += numOfDice.get(1)*100;
            }
            else if(numOfDice.get(1) == 3) {
                points += 1000;
            }
            else if(numOfDice.get(1) == 4) {
                points += 2000;
            }
            else if(numOfDice.get(1) == 5) {
                points += 3000;
            }
            else if(numOfDice.get(1) == 6) {
                points += 5000;
            }
        }
        else {

        if(i == 5) {
            if(numOfDice.get(5) < 3) {
                points += 50*numOfDice.get(5);
                continue;
            }
        }
        //All else
        if(numOfDice.get(i) == 3) {
            points += 100*i;
        }
        else if(numOfDice.get(i) == 4) {
            points += 200*i;
        }
        else if(numOfDice.get(i) == 5) {
            points += 300*i;
        }
        else if(numOfDice.get(i) == 6) {
            points += 400*i;
        }
    }
}
    return points;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2011-07-19
    • 2011-04-24
    • 1970-01-01
    相关资源
    最近更新 更多