如何不允许负数?
问题描述:
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;
}
}
答
应该考虑minValue
和maxValue
是正
if (v >= minValue && v <= maxValue)
如果minValue
是否定的,那么你可以添加更多的检查
if(v >= 0)
是什么问题? – 2013-04-25 19:55:47