2013-04-25 76 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