0

I'm making a rock paper scissors game, and in this game there is a nested for loop so that the players challenge each other, the outer loop is supposed to loop through all the players starting with player 1, while the inner loop starts at the last player and moves to the outer loop player, this sounds simple enough but for some reason the loop I am trying to use doesn't work. Here is what the loop looks like:

for (j = 0; j < playerNum; j++)
                {
                    for (k = (playerNum - 1); k > j; k--)
                    {
                        if(j == k)
                        {
                            continue;
                        }
                            players[j].challenge(players[k]);   // challenge player[j] with player[k]
                            /* Save current player's results to gameResults total.
                            * Put each player result on a new line.
                            */
                            gameResults += players[j].getWins() + "\n";
                    }
            }
7
Contributors
8
Replies
9
Views
11 Years
Discussion Span
Last Post by masijade
0
for (j = 0; j < playerNum; j++)
                {
                    for (k = (playerNum - 1); k > j; k--)
                    {
                        // j==k test not needed, k>j
                            players[j].challenge(players[k]);   // challenge player[j] with player[k]
                    }
                    /* Save current player's results to gameResults total.
                     * Put each player result on a new line.
                     */
                    gameResults += players[j].getWins() + "\n";
            }
0

what nested loops in java
1. sample output
*
*
*
*
*
2. enter a number: 3 input
*
*
*
3.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

0

what nested loops in java
1. sample output
*
*
*
*
*
2. enter a number: 3 input
*
*
*
3.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Start a new thread and show some code. For your problems you will need for loops.
For the first use a for loop with fixed upper limit. For the second use the input as upper limit.
For the second, you will need 2 loops one inside the other.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.