I’m trying to make a program in which values increase as a button is pressed, and the progress is shown in a progress bar. The min is 0 and the max is 100 and I coded it so that if the value reaches above 100, make the value equal 100. However, it keeps going over 100 and giving me an error. I’m new to programming and I’m not sure where my error is.
if (happyLevel < 100)
{
happyLevel = happyLevel + 4;
}
else if (happyLevel > 100)
{
happyLevel = 100;
}
if (hungryLevel < 100)
{
hungryLevel = hungryLevel + 4;
}
else if (hungryLevel > 100)
{
hungryLevel = 100;
}
if (healthLevel < 100)
{
healthLevel = healthLevel + 4;
}
else if (healthLevel > 100)
{
healthLevel = 100;
}
You’re checking using an
else if, which means that the+ 4could be incrementing any variable past 100 if you have a number higher than 96 within it. A quick way to fix it is to remove all theelses before theifs so you do the check no matter what.