2 Replies - 184 Views - Last Post: 03 October 2012 - 12:54 PM Rate Topic: -----

#1 pauljones85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

while loop inside the for loop

Posted 03 October 2012 - 11:30 AM

I am wondering what i am doing wrong. I am trying to have a while loop inside the for-looop that only continues until one of the players have a status of in
public void checkWinner(){

int sel = r.nextInt(players.length);
players[sel].setStatus(Player.Status.OUT);

while(players.length==1 && players[i].getStatus==Player.Status.IN){
System.out.println("\nThis is how we stand...");
for(int i=0;i<players.length;i++){
System.out.println(players[i].getName()+" is "+players[i].getStatus());
} break;
}




}

public void checkWinner(){

int sel = r.nextInt(players.length);
players[sel].setStatus(Player.Status.OUT);

while(players.length==1 && players[i].getStatus==Player.Status.IN){
System.out.println("\nThis is how we stand...");
for(int i=0;i<players.length;i++){
System.out.println(players[i].getName()+" is "+players[i].getStatus());
} break;
}




} 


I am wondering what i am doing wrong. I am trying to have a while loop inside the for-looop that only continues until one of the players have a status of in

public void checkWinner(){

int sel = r.nextInt(players.length);
players[sel].setStatus(Player.Status.OUT);

while(players.length==1 && players[i].getStatus==Player.Status.IN){
System.out.println("\nThis is how we stand...");
for(int i=0;i<players.length;i++){
System.out.println(players[i].getName()+" is "+players[i].getStatus());
} break;
}




} 


Sorry about not using the correct code tags

Is This A Good Question/Topic? 0
  • +

Replies To: while loop inside the for loop

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: while loop inside the for loop

Posted 03 October 2012 - 11:56 AM

Firstly you want the outer loop to run players.length-1 times so there are only 1 left that are "in", then you don't want to continue to the next iteration until a player that was not previously removed is removed, this is what the second loop is for. Below follows an example of one way of many, that you could do it.

for(int i = 0; i < players.length-1; i++)
{
   boolean playerRemoved = false;
   while(!playerRemoved) //as long as a player isn't removed.
   {
      int sel = r.nextInt(players.length);
      if(players[sel].getStatus() == Player.Status.IN)
      { 
         players[sel].setStatus(Player.Status.OUT);
         playerRemoved = true;
      }
   }  
}

Was This Post Helpful? 1
  • +
  • -

#3 pauljones85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: while loop inside the for loop

Posted 03 October 2012 - 12:54 PM

View PostKakerergodt, on 03 October 2012 - 11:56 AM, said:

Firstly you want the outer loop to run players.length-1 times so there are only 1 left that are "in", then you don't want to continue to the next iteration until a player that was not previously removed is removed, this is what the second loop is for. Below follows an example of one way of many, that you could do it.

for(int i = 0; i < players.length-1; i++)
{
   boolean playerRemoved = false;
   while(!playerRemoved) //as long as a player isn't removed.
   {
      int sel = r.nextInt(players.length);
      if(players[sel].getStatus() == Player.Status.IN)
      { 
         players[sel].setStatus(Player.Status.OUT);
         playerRemoved = true;
      }
   }  
}

I really appreciate the help that you gave me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1