c# - 如果数字 >= 24,则获得下一个可被 12 整除的数字
问题描述
我正在为游戏开发库存系统。
如果玩家库存是空的或少于 24 个项目,我想显示 36 个插槽。
如果库存有 24 件或更多物品,我想找到下一个可被 12 整除的数字并使用它。基本上我总是希望允许至少 12 个额外的插槽,总共至少 36 个。
这是我尝试过的代码,但没有给我想要的结果:
int testInventoryCount = 29;
int itemSlotCount = 36;
int itemSlotCount2 = 36;
int itemSlotCount3 = 36;
if (testInventoryCount >= 24)
{
itemSlotCount = ((testInventoryCount / 12) + 1) * 12;
itemSlotCount2 = (testInventoryCount + 12) - (testInventoryCount % 12);
itemSlotCount3 = (testInventoryCount + 12 / 2) / 12 * 12;
}
Debug.Log(testInventoryCount);
Debug.Log(itemSlotCount);
Debug.Log(itemSlotCount2);
Debug.Log(itemSlotCount3);
他们都没有给我正确的价值。
例如,这就是我想要的:
If the inventory has 0 items, have 36 slots.
If the inventory has 20 items, have 36 slots.
If the inventory has 26 items, have 48 slots.
If the inventory has 30 items, have 48 slots.
If the inventory has 34 items, have 48 slots.
If the inventory has 60 items, have 72 slots.
If the inventory has 66 items, have 84 slots.
ETC...
我数学很差。哈尔普。
解决方案
我编写了一个简单的函数来获取必要的插槽数量。
public static int get_slot_count(int item_count)
{
return Math.Max(36,(item_count / 12 + 2) * 12);
}
我测试并得到以下结果。
0 items / 36 slots
1 items / 36 slots
2 items / 36 slots
3 items / 36 slots
4 items / 36 slots
5 items / 36 slots
6 items / 36 slots
7 items / 36 slots
8 items / 36 slots
9 items / 36 slots
10 items / 36 slots
11 items / 36 slots
12 items / 36 slots
13 items / 36 slots
14 items / 36 slots
15 items / 36 slots
16 items / 36 slots
17 items / 36 slots
18 items / 36 slots
19 items / 36 slots
20 items / 36 slots
21 items / 36 slots
22 items / 36 slots
23 items / 36 slots
24 items / 48 slots
25 items / 48 slots
26 items / 48 slots
27 items / 48 slots
28 items / 48 slots
29 items / 48 slots
30 items / 48 slots
31 items / 48 slots
32 items / 48 slots
33 items / 48 slots
34 items / 48 slots
35 items / 48 slots
36 items / 60 slots
37 items / 60 slots
38 items / 60 slots
39 items / 60 slots
40 items / 60 slots
41 items / 60 slots
42 items / 60 slots
43 items / 60 slots
44 items / 60 slots
45 items / 60 slots
46 items / 60 slots
47 items / 60 slots
48 items / 72 slots
49 items / 72 slots
我很好奇为什么 24 个项目需要超过 36 个插槽。如果您只需要额外的 12 个插槽,那么以下会更好,并给出更合理的结果。
public static int get_slot_count(int item_count)
{
return Math.Max(36,((item_count - 1) / 12 + 2) * 12);
}
试验结果。
0 items / 36 slots
1 items / 36 slots
2 items / 36 slots
3 items / 36 slots
4 items / 36 slots
5 items / 36 slots
6 items / 36 slots
7 items / 36 slots
8 items / 36 slots
9 items / 36 slots
10 items / 36 slots
11 items / 36 slots
12 items / 36 slots
13 items / 36 slots
14 items / 36 slots
15 items / 36 slots
16 items / 36 slots
17 items / 36 slots
18 items / 36 slots
19 items / 36 slots
20 items / 36 slots
21 items / 36 slots
22 items / 36 slots
23 items / 36 slots
24 items / 36 slots
25 items / 48 slots
26 items / 48 slots
27 items / 48 slots
28 items / 48 slots
29 items / 48 slots
30 items / 48 slots
31 items / 48 slots
32 items / 48 slots
33 items / 48 slots
34 items / 48 slots
35 items / 48 slots
36 items / 48 slots
37 items / 60 slots
38 items / 60 slots
39 items / 60 slots
40 items / 60 slots
41 items / 60 slots
42 items / 60 slots
43 items / 60 slots
44 items / 60 slots
45 items / 60 slots
46 items / 60 slots
47 items / 60 slots
48 items / 60 slots
49 items / 72 slots
推荐阅读
- dictionary - 从地图派生关联容器
- gramex - 是否可以将请求路径中的动态值分配给 FormHandler gramex-config 条目的“表”属性?
- huggingface-transformers - Huggingface AutoTokenizer 无法从本地路径加载
- ruby-on-rails - Shopify 控制器的 Rspec?卡在 302 上进行身份验证
- java - 从参与者调用的服务中长时间运行的进程
- rust - 如何编写支持+=操作的特征绑定,其右手是Rust中复杂情况下的引用
- json - 尝试将 json 文件读入 scala 中的 Map[String,Object]
- amazon-web-services - 如何将响应放在 websocket api 网关后面的单独 lambda 中?
- python - 我可以腌制张量流模型吗?
- python - 为什么 Python 在 Windows 10 PC 中看不到新建的 CV2 包?