문제

 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;
           }
    }
도움이 되었습니까?

해결책 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)

다른 팁

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top