C# 如果编号>;=24,得到下一个可被12整除的数

C# 如果编号>;=24,得到下一个可被12整除的数,c#,math,C#,Math,我正在为一个游戏设计一个库存系统 如果播放器库存为空或少于24个项目,我想显示36个插槽 如果库存有24个或更多的项目,我想找到下一个可以被12整除的数字并使用它。基本上,我总是想允许至少12个额外的插槽,总共至少36个 以下是我尝试过的代码,它没有给我想要的结果: int testInventoryCount = 29; int itemSlotCount = 36; int itemSlotCount2 = 36; int itemSlotCount3 = 3

我正在为一个游戏设计一个库存系统

如果播放器库存为空或少于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.
等等


我数学很差。Halp.

我编写了一个简单的函数,用于获取所需插槽的数量

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
我很好奇为什么你需要超过36个插槽来放置24件物品。如果您只需要额外的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

请更新您的示例,以显示如果库存为12、24、36和48,插槽应该是什么。