944,038 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 15932
  • Java RSS
Jul 25th, 2005
0

nested for loop problem

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  1. for (j = 0; j < playerNum; j++)
  2. {
  3. for (k = (playerNum - 1); k > j; k--)
  4. {
  5. if(j == k)
  6. {
  7. continue;
  8. }
  9. players[j].challenge(players[k]); // challenge player[j] with player[k]
  10. /* Save current player's results to gameResults total.
  11.   * Put each player result on a new line.
  12.   */
  13. gameResults += players[j].getWins() + "\n";
  14. }
  15. }
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
the b is offline Offline
42 posts
since Sep 2004
Feb 10th, 2008
-1

Re: nested for loop problem

wow
Reputation Points: 9
Solved Threads: 0
Newbie Poster
ssniper101 is offline Offline
1 posts
since Feb 2008
Feb 10th, 2008
0

Re: nested for loop problem

java Syntax (Toggle Plain Text)
  1. for (j = 0; j < playerNum; j++)
  2. {
  3. for (k = (playerNum - 1); k > j; k--)
  4. {
  5. // j==k test not needed, k>j
  6. players[j].challenge(players[k]); // challenge player[j] with player[k]
  7. }
  8. /* Save current player's results to gameResults total.
  9.   * Put each player result on a new line.
  10.   */
  11. gameResults += players[j].getWins() + "\n";
  12. }
Reputation Points: 10
Solved Threads: 1
Newbie Poster
dandan is offline Offline
12 posts
since Jan 2006
Feb 11th, 2008
0

Re: nested for loop problem

i find it hard to get your point!
Reputation Points: 10
Solved Threads: 2
Newbie Poster
striker3344 is offline Offline
19 posts
since Dec 2007
Aug 31st, 2010
0
Re: nested for loop problem
what is nested loops in java...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jayr apuhin is offline Offline
3 posts
since Aug 2010
Aug 31st, 2010
0
Re: nested for loop problem
sample output * * * * *
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jayr apuhin is offline Offline
3 posts
since Aug 2010
Aug 31st, 2010
0
Re: nested for loop problem
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jayr apuhin is offline Offline
3 posts
since Aug 2010
Aug 31st, 2010
0
Re: nested for loop problem
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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Aug 31st, 2010
0
Re: nested for loop problem
Killing this zombie.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
Next Thread in Java Forum Timeline: Help please!: Payroll Program part 3





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC