2013-04-25 73 views
-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

是什么问题? – 2013-04-25 19:55:47

回答

1

应该考虑minValuemaxValue是正

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

如果minValue是否定的,那么你可以添加更多的检查

if(v >= 0) 
2

假设minValue大于零,您应该更改||。到& &以同时检查范围的两端。

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

如果minValue(最小值)并不一定是大于零

if (v >= minValue && v <= maxValue && v >= 0) 
+1

不应该是'&&''代替'||'来代表最小范围内的值吗? – Smit 2013-04-25 19:58:23

+0

你是对的,斯密特。虽然这个错误是从问题中复制过来的。 – Brian 2013-04-25 20:01:40