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

2012/12/15

>= and <= in same statement

davemib123 davemib123

2012/12/15

#
Hi guys, trying to set up a powerup selection where if the number is within a certain range a powerup type is given. I have this at the moment:
powerupSpawn ++;   
        if (powerupSpawn > 800){
            int powerupSelection = Greenfoot.getRandomNumber(10);
            if (powerupSelection >= 8){
                addObject(new HealthPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
            if (powerupSelection <= 4){
                addObject(new WeaponPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
            if (powerupSelection >= 5){
                addObject(new ShieldPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
        }
the very last powerup for shield how do I get it so that the number should be "greater or equal to 5 but less or equal to 7"?
davemib123 davemib123

2012/12/15

#
ive tried this, seems to work:
 if ((powerupSelection >= 5) & (powerupSelection <=7)){
                addObject(new ShieldPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
is that correct or is there a better way of getting it done?
Gevater_Tod4711 Gevater_Tod4711

2012/12/15

#
normaly a logical and is && but the idea is right I think & is the bit operator but I don't know if there is a difference in this case.
actinium actinium

2012/12/16

#
if (x >= 5 && x <=7)
is what most people would use. The difference between using & and && is that in the case of using && if the first expression is false the second expression is never executed. In your case when using & both expressions are evaluated whatever the result of the first evaluation.
You need to login to post a reply.