我正在从移除端点获取数据。数据响应仅包含项目总数,但不包含页面总数。如果我只有总项目数和每页项目数,我如何计算总页数?我需要这个,因为我正在尝试设置分页库
例如:
perPage = 10
totalItemCount = 10
totalPages = 1
其思想是totalItemCount大于10+1但小于或等于20,totalPages将为2;并且totalItemCount>10+10+1但低于或等于30,totalPages=3,依此类推…
我无法使用Kotlin 进行良好的算法计算
这似乎有效。我试过了https://pl.kotl.in/hUDoamkG3:
fun main()
{
val listOfTotalItemCounts = listOf(0, 1, 9, 10, 15, 19, 20, 21, 124)
val perPage = 10
for (totalItemCount in listOfTotalItemCounts)
{
var result = (totalItemCount / perPage)
result = if (totalItemCount % perPage == 0) result else (result + 1)
print("When totalItemCount = ")
print(totalItemCount)
print(" ---> result = ")
println(result)
}
}
它产生以下输出:
When totalItemCount = 0 ---> result = 0
When totalItemCount = 1 ---> result = 1
When totalItemCount = 9 ---> result = 1
When totalItemCount = 10 ---> result = 1
When totalItemCount = 15 ---> result = 2
When totalItemCount = 19 ---> result = 2
When totalItemCount = 20 ---> result = 2
When totalItemCount = 21 ---> result = 3
When totalItemCount = 124 ---> result = 13