已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。
2天前关闭。
Improve this question
基本上我正在与一个游戏,所以我想要一个简单的方法来确定项目金额。
例如,我正在创建一个ArrayList<Item>();
,并标识如下项目:
//Item(itemId, itemAmount);
new Item(ItemsList.COINS, 1_000_000);//this is 1m of coins
new Item(ItemsList.FISH, 2000);//this is 2k of fish
我想要一个更简单的方法而不是把金额写得像
new Item(ItemsList.COINS, Amounts.1M);
new Item(ItemsList.FISH, Amounts.2k);`
像这样,我想就如何创建类金额的指导,并继续下去?
当然,我不打算创建一个枚举与所有的值一样,什么是聪明的方式来完成这项任务。请帮助我,谢谢!
2条答案
按热度按时间hwazgwia1#
您可能想尝试这样的方法:
然后你可以这样使用它:
但是,我个人更喜欢使用常量(并在三位数后插入
_
),例如:或者类似的东西
(and在类
Amounts
中定义静态常量public static int MILLION = 1_000_000;
)az31mfrm2#
您可以使用下面的函数将数字转换为您的格式,并可以在您的代码中使用,根据您的要求进行一些更改-