This site requires JavaScript, please enable it in your browser!
Greenfoot back
Baenefin
Baenefin wrote ...

2013/11/10

My average height is not working.

Baenefin Baenefin

2013/11/10

#
Hi to all, My average height is not working for some reason. I get the height from all the Ballon in my sceneario (via the light hoogte) And I get the average height. then i set them to the average height. But for some reason they dont go to the average height. What am I missing?
      public void setAlleBallonnenOpGemiddeldeHoogte()
    {
        //bepaal gem hoogte
        
        int totaleHoogte = 0;
        List<Ballon> alles = getObjects(Ballon.class);
        for(Ballon ballon : alles)  
        {  
            int hoogte = ballon.getY();
            totaleHoogte = totaleHoogte + hoogte;
        }  
        
        int gemiddelde = totaleHoogte /  alles.size();
        //zet op gemiddelde hoogte
        for(Ballon ballon : alles)  
        {  
            int x = ballon.getX();
            int y = ballon.getY();
            
            ballon.setLocation(x, y);
        }  
        
    }
GreenHouse GreenHouse

2013/11/10

#
i think you mean ballon.setLocation(x, totaleHoogte); instead of ballon.setLocation(x, y); in line 20
Baenefin Baenefin

2013/11/10

#
That just sets them on the bottom of the sceneario, not the average.
GreenHouse GreenHouse

2013/11/10

#
for(Ballon ballon : alles)    
    {    
        int x = ballon.getX();  
        int y = ballon.getY();  
          
        ballon.setLocation(x, y);  
    }  
What does this do? You set the ballon's position; to its position.
Baenefin Baenefin

2013/11/10

#
First i get all the heights
List<Ballon> alles = getObjects(Ballon.class);
for(Ballon ballon : alles)  
        {  
            int hoogte = ballon.getY();
            totaleHoogte = totaleHoogte + hoogte;
        }  
then i get the average height
int gemiddelde = totaleHoogte /  alles.size();
and i set them to that height
for(Ballon ballon : alles)  
        {  
            int x = ballon.getX();
            int y = ballon.getY();
            
            ballon.setLocation(x, y);
        }  
GreenHouse GreenHouse

2013/11/10

#
?
for(Ballon ballon : alles)    
        {    
            int x = ballon.getX();  // <- ballon's x position
            int y = ballon.getY(); // <- ballon's y position 
              
            ballon.setLocation(x, y);   // set ballon's pos; to its pos!!!
        } 
You do not even use gemiddelde; only calculate it. 'IDE Warning: The local variable gemiddelde is declared, but never used.'
danpost danpost

2013/11/10

#
From your initially posted code, change line 18 to:
int y = gemiddelde;
Baenefin Baenefin

2013/11/10

#
Greenhouse, thanks for pointing that out. danpost, that was my next step. I couldn't figure it out. Thank you both.
You need to login to post a reply.