vendredi 26 décembre 2014

Java : If statement to process amount you want whilst discarding the rest?

I have the following script :



public void addcheese(int addcheese)
{
if (cheeseamount + addcheese < mincapacity){
System.out.println("Sorry no more cheese can be removed.");
}
else if(cheeseamount + addcheese <= maxcapacity)
{
if ((cheeseamount + addcheese > 1900) && (cheeseamount + addcheese < 2000))
{
System.out.println("Warning : The box is nearing it's maximum capacity.") ;
}
cheeseamount = cheeseamount + addcheese;
}

else {
System.out.println("This storage box has reached its maximum capacity. You cannot fill it any more");}
}


(Note : In constructor maxcapacity = 2000)


The script works as it should, however there is a problem in that if i was to input a number like 3400, it would still print the message "This storage box has reached its max..." without adding anything. How would i edit the script to, in this case, add to the box up until the maxcapacity, and discard the excess amount. Thank you.


Aucun commentaire:

Enregistrer un commentaire