我有一個返回我列表int類型的功能取決於價值的一部分:除以3,在C#中的功能
private List<int> GetColumn(int total, int column)
{
List<int> retList = new List<int>();
if (total <= 0)
{
return retList;
}
if (column < 0 || column > 2)
{
column = 0;
}
int pageSize = total/3;
int startIndex = column * pageSize;
int endIndex = column * pageSize + pageSize;
if (endIndex > total)
{
endIndex = total;
}
for (int i = startIndex; i < endIndex; i++)
{
retList.Add(i);
}
return retList;
}
,但它的工作原理錯誤的,因爲: GetColumn(17,0)
它返回[0,1,2,3,4],但應返回[0,1,2,3,4,5]
GetColumn(17,1) - [6,7,8, (17,2) - [12,13,14,15,16]
對於16個用於GetColumn(17,2)的
它應返回:GetColumn(16,0) - [0,1,2,3,4,5]
對於GetColumn(16,1) - [6,7,8,9,10]
返回: GetColumn(16,2) - [11,12,13,14,15]
我應該改變什麼功能?謝謝!
您的第一個示例爲(17,1)調用返回6個元素,但爲(17,0)和(17,2)調用返回5個元素。如果元素的數量不能被3整除,這個邏輯將如何工作?應該有一個隨機的「頁面」獲得額外的元素,還是總是中間的?如果有兩個「額外」元素呢?中間和第一個,還是中間和最後一個? – 2012-03-09 06:45:14
'int'除以'int'仍然是一個'int'。至少有一個操作數必須是'float' /'double'以使結果爲非'int'。 – 2012-03-09 06:45:31
不,在這種情況下不需要劃分多於3個 – ihorko 2012-03-09 07:10:30