mercredi 25 décembre 2019

fit a number into the range if if chained

I have a total variable and I have a range of 250 out of 250 I need to know in which range is the variable, but in a range of 15250 would have to do more than 61 if chained, has another way to eliminate this repetition of ifs? Thanks for the help. example

 public void ValidTravel(int Traveled)
 {
    int total = 15250
    int range = 250   

     if (Traveled>= 1 && Traveled<= 250)
        {                
           return 250;
        }

     if (Traveled>= 251 && Traveled<= 500)
        {                
           return 500;
        }

     if (Traveled>= 501 && Traveled<= 750)
        {                
           return 750;
        }

     if (Traveled>= 751 && Traveled<= 1000)
        {                
           return 1000;
        }

}

Aucun commentaire:

Enregistrer un commentaire