-4
 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;
           }
    }
4

2 に答える 2

2

minValue が 0 より大きいと仮定すると、|| を変更する必要があります。&& にすると、範囲の両端が同時にチェックされます。

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

minValue がゼロより大きいとは限らない場合

if (v >= minValue && v <= maxValue && v >= 0)
于 2013-04-25T19:56:35.577 に答える
1

それは考慮されるべきでminValueありmaxValue、肯定的です

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

が負の場合minValueは、もう 1 つチェックを追加できます

if(v >= 0)
于 2013-04-25T19:59:59.707 に答える