2 Replies - 192 Views - Last Post: 03 October 2012 - 07:58 AM Rate Topic: -----

#1 pauljones85  Icon User is offline

  • New D.I.C Head

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

selecting and storing a random array element

Posted 03 October 2012 - 06:26 AM

I am trying to select a random element from an array and change they status to out. Sometimes it randomly changes all the statuses to OUT Or sometimes keep all with an IN status. I want it do select one random element and change the status to out.

import java.util.*;
public class MusicalChairs {

    Player [] players;
    Random r = new Random();
    public static void main(String[] args){
      
     
      MusicalChairs mc = new MusicalChairs();
      
     
        
          mc.setUpGame(args);
          mc.showStatus();
          //mc.winner();
          
        }
        //fill up the player array with players and their status
        public void setUpGame(String [] p){
        System.out.println("This is how we stand.......");
        //intitailizing players array
        players = new Player[p.length];

             for (int i = 0; i < p.length; i++){
             players[i] = new Player(p[i]);
             players[i].setStatus(Player.Status.IN); // setting all players status to IN
             System.out.println(players[i].getName()+" is "+ players[i].getStatus());
             }
            
            
            
        }

public void showStatus(){
           
            //fill the array with players and select a player to be out
                System.out.println("\nThis is how we stand...");
                for(int i=0;i <players.length;i++){
                 int index = r.nextInt(players.length);
                
                  if(players[index].equals(players[i])) //I am not understanding why it doesn't select only one element and changing the status to Out.
                  {
                    players[i].setStatus(Player.Status.OUT);
                  
                  }
                
                   else{
                    players[i].setStatus(Player.Status.IN);
                   }
                
                System.out.println(players[i].getName()+" is "+players[i].getStatus());
               
               }
            
        }
      public void winner(){
           for(int i=0;i <players.length;i++){ 
                if(players[i].getStatus()==Player.Status.IN){
                    System.out.println(players[i].getName()+" is the winner");
                }
            }
            
        }
      
    }

class Player{
    enum Status{IN,OUT};
    private String name;
    private Status status;
    
   public Player(String n){
       name=n;
   }
   
   public String getName(){
       return name;
   }
  
   public void setStatus(Status s){
       status=s;
   }
   
   public Status getStatus(){
       return status;
   }
   
} 


Is This A Good Question/Topic? 0
  • +

Replies To: selecting and storing a random array element

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

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

Re: selecting and storing a random array element

Posted 03 October 2012 - 07:41 AM

It doesnt work correctly because you do it in a for-loop, so in other words you set a player to out for every iteration of the loop only if it matches the iteration index. This will produce a bit of unpredictable outcome, where between none and all players will be "out".
All you need is:
int index = r.nextInt(players.length);
players[index].setStatus(Player.Status.OUT);


..unless you want one player to be "out" for every iteration of the loop, then you will have to have a while loop inside the for-looop that only continues after ONE player has been removed but will generate new random numbers till it finds a player that hasn't previously been set to "out".
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: selecting and storing a random array element

Posted 03 October 2012 - 07:58 AM

View PostKakerergodt, on 03 October 2012 - 07:41 AM, said:

It doesnt work correctly because you do it in a for-loop, so in other words you set a player to out for every iteration of the loop only if it matches the iteration index. This will produce a bit of unpredictable outcome, where between none and all players will be "out".
All you need is:
int index = r.nextInt(players.length);
players[index].setStatus(Player.Status.OUT);


..unless you want one player to be "out" for every iteration of the loop, then you will have to have a while loop inside the for-looop that only continues after ONE player has been removed but will generate new random numbers till it finds a player that hasn't previously been set to "out".

Thanks a lot I fully understand what I was doing wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1