954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

nested for loop problem

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";
                    }
            }
the b
Light Poster
42 posts since Sep 2004
Reputation Points: 12
Solved Threads: 0
 
ssniper101
Newbie Poster
1 post since Feb 2008
Reputation Points: 9
Solved Threads: 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";
            }
dandan
Newbie Poster
12 posts since Jan 2006
Reputation Points: 10
Solved Threads: 1
 

i find it hard to get your point!

striker3344
Newbie Poster
19 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

what is nested loops in java...

jayr apuhin
Newbie Poster
3 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

sample output * * * * *

jayr apuhin
Newbie Poster
3 posts since Aug 2010
Reputation Points: 10
Solved Threads: 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

jayr apuhin
Newbie Poster
3 posts since Aug 2010
Reputation Points: 10
Solved Threads: 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.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Killing this zombie.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You