Home > Software engineering >  Java: Calculating pairs in array
Java: Calculating pairs in array

Time:11-24

I have tried to build mini-game, with an random generated array of 10,20,50,200,1000. It calculates an amount of pairs, however it calculates in a certain weird way. Where is if I have 3 of 10 - it is calculated as 2 pairs, and if I have 4 of 10 it is calculated as 5 pairs. Basically, I need just if it is 4 of 10 then it should have 2 pairs, if it is 6 of 10 it should have 3 pairs. Any tips, advices are appreciated

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

}

CodePudding user response:

your program counts how many times the same values appears futher on in the list.

In order to achive what you intended to do you could try different approaches for example:

1 create list of already used numbers

2 use "Array lists" to remove already used values or just change them to 0

3 Create list of occurences of each result

I present method number 3 below:

    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  ;
    }
  • Related