How to compare variables in a loop, java

Sol

  • Switch player loop and rounds loop.
  • In each round loop maintain a max and update it with max value and player.
  • Modify printresult a little to remove round.

Loop and max:

    for (int r = 0; r < rounds; r++) { // loop for number of rounds
        int max = 0;
        int max_p = 0;
        System.out.println("Round " + r + ": ");
        for (int p = 0; p < players; p++) { //loop for players
            int diceArray[] = new int[3];
            //...
            thrdiesc.printResult(p, r);
            if (thrdiesc.total > max) {
                max = thrdiesc.total;
                max_p = p;
            }
        }
        System.out.println("Winner is player " + (max_p + 1) + "\n");
    }

PrintResult Method:

    public void printResult(int p, int r) {
        System.out.println("player " + (p + 1) + "   " + die1 + " " + die2 + " " + die3 + " " + "points: " + total);
    }

Misc

  • Indent Code properly.
  • Be careful while copying. (See the prompt)

Leave a Comment