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

Recommended Answers

All 8 Replies

wow

commented: Pointless post that added nothing to thread -1
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";
            }

i find it hard to get your point!

what is nested loops in java...

sample output * * * * *

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

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.

Killing this zombie.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.