Question

 public boolean add(int v) 
    {
        if (count < list.length)      //  if there is still an available slot in the array 
           {
           if (v >= minValue || v <= maxValue) // if the value is within range
                {
                list[count] = v;    // add the value to the next available slot
                count++;          // increment the counter
                return true;       // all okay ; Value added
                }
           else 
                {
                System.out.println("Error: The value is out of range. Value not added");
                return false;
                }
           }
        else 
           {
           System.out.println("Error: The list is full. Value not added.");
           return false;
           }
    }
Était-ce utile?

La solution 2

It should be considering minValue and maxValue are positive

if (v >= minValue && v <= maxValue)

if minValue is negative then you can add one more check

if(v >= 0)

Autres conseils

Assuming minValue is greater than zero, you should change the || to an && to check both ends of the range at the same time.

if (v >= minValue && v <= maxValue)

If minValue is not necessarily greater than zero

if (v >= minValue && v <= maxValue && v >= 0)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top