【问题标题】:Java: Calculating pairs in arrayJava:计算数组中的对
【发布时间】:2021-11-23 19:35:11
【问题描述】:

我尝试构建一个小游戏,随机生成数组 10,20,50,200,1000。它计算成对的数量,但是它以某种奇怪的方式计算。如果我有 10 个中的 3 个 - 它被计算为 2 对,如果我有 10 个中的 4 个,它被计算为 5 对。基本上,我只需要 4 of 10 那么它应该有 2 双,如果它是 6 of 10 它应该有 3 双。任何提示,建议表示赞赏

int[] bonusGame = new int[5]; // bonus game array
int bonusGameSum=0; // amount of prize for bonus game 
int randomChance = r.nextInt(100); // generating chance
for (int newNum = 0; newNum<5 ;newNum++) { // getting chances for numbers
    int chanceGen = r.nextInt(100); // generating chance 
    if (chanceGen <= 50) { // generating chance for 10 
        bonusGame[newNum] = 10;
    }
    else if (chanceGen <= 77) { // generating chance for 20
        bonusGame[newNum] = 20;
    }
    else if (chanceGen <= 92) { // generating chance for 50 
        bonusGame[newNum] = 50;
    }
    else if (chanceGen <= 98) { // generating chance for 200 
        bonusGame[newNum] = 200;
    }
    else {                       // generating chance for 1000
        bonusGame[newNum] = 1000;
    }
    
    }
for (int z: bonusGame) {
    System.out.println(z);
}
for (int f = 0; f < bonusGame.length - 1; ++f) { // checking for pairs in array and adding up to winning sum
    for (int j = f + 1; j < bonusGame.length; ++j) {
        if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 10 ) { // if 10 has pairs
            System.out.println(" You won 10 euro ");
            bonusGameSum +=10;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 20 ) { // if 20 has pairs
            System.out.println(" You won 20 euro ");
            bonusGameSum +=20;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 50 ) { // if 50 has pairs
            System.out.println(" You won 50 euro ");
            bonusGameSum +=50;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 200 ) { // if 200 has pairs
            System.out.println(" You won 200 euro ");
            bonusGameSum +=200;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 1000 ) { // if 1000 has pairs
            System.out.println(" You won 1000 euro ");
            bonusGameSum +=1000;
        }
    }

}

【问题讨论】:

  • 至少对我来说,你不清楚你在哪里为“对”计算编码。
  • 请提供预期输出与实际输出的示例。我不了解您的代码的目标或您要实现的目标

标签: java arrays


【解决方案1】:

您的程序会计算相同值在列表中出现的次数。

为了实现您的目标,您可以尝试不同的方法,例如:

1 创建已使用号码的列表

2 使用“数组列表”删除已使用的值或将它们更改为 0

3 创建每个结果的出现列表

我在下面介绍方法 3:

    Random r = new Random();
    int[] bonusGame = new int[5]; // bonus game array


    for (int newNum = 0; newNum < bonusGame.length; newNum++)
    { // getting chances for numbers
        int chanceGen = r.nextInt(100); // generating chance

        if (chanceGen <= 50)
        { // generating chance for 10
            bonusGame[newNum] = 10;
        }
        else if (chanceGen <= 77)
        { // generating chance for 20
            bonusGame[newNum] = 20;
        }
        else if (chanceGen <= 92)
        { // generating chance for 50
            bonusGame[newNum] = 50;
        }
        else if (chanceGen <= 98)
        { // generating chance for 200
            bonusGame[newNum] = 200;
        }
        else
        { // generating chance for 1000
            bonusGame[newNum] = 1000;
        }
    }

    for (int z: bonusGame)
    {
        System.out.println(z);
    }

    int[] pool = {10, 20, 50, 200, 1000}; // possible winnings
    int[] pairs = {0, 0, 0, 0, 0}; // array counting how many times we scored each number
    for (int value : bonusGame)  // search through random results
    {
        int index = 0; // matching random result with correct element of the pool
        for (int size : pool) // comparing each result with our possible winnings
        {
            if (value == size)
            {
                pairs[index]++; // increase number of successes by one
                break;
            }
            index++;
        }
    }

    int index = 0;
    for (int value : pairs)  // search through created list of values for each random result
    {
        while (value >= 2) // when such result occurred at least twice
        {
            value -= 2;
            System.out.println("You won " + pool[index] + " euro");
        }
        index++;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2017-03-24
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多