mardi 15 mars 2022

if, else statement within an object constructor always resulting in the first branch executing [closed]

My constructor for my sheep object randomly assigns the boolean vlaue isFat depending on the result of Math.random but it always results in the value being true.

private boolean isFat;

public Sheep()
{
    alive = true;
    
    if (Math.random() < 0.5)   { 
        this.isFat = true;
    }
    else   {
        this.isFat = false;
    }
    }

I have had the same result using different random number generators and it doen't make a difference even when I set if to an impossible statement such as if (Math.random > 1.0) I am a beginner both in coding and using stackoverflow so I appreciate any help.




Aucun commentaire:

Enregistrer un commentaire