位数以及是哪几位整数
示例 int i=185;
然后我想得到 'i' 包含 3 位数字,这些数字是 1,8 和 5。
Example int i=185;
Then I want to get that 'i' contains 3 digits and those digits are 1,8, and 5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
执行此操作的简单方法是转换为与区域设置无关的字符串,然后查看字符串中的每个字符。我不会给出最终的解决方案,以防万一这是家庭作业,但这里有一些重要的 API...
转换为字符串:
处理负数:
字符串的长度:
获取字符串的第 i 个字符:
The easy way to do this is by converting to a locale-agnostic string, then looking at each character in the string. I am not giving the final solution in case this is homework, but here are some important APIs...
Converting to string:
Handling negatives:
Length of a string:
Getting the i-th character of a string:
一种方法是:
但我认为你需要更通用的东西?尝试通过字符串
和更高级:
然后你就可以在集合上工作了。
One way would be:
But i think you need something more generic? Try via string
And more advanced:
Then you can work on the set.
对于整数
i
,小数位数也由 Math.ceil(Math.log10(i)) 给出。The number of decimal digits is also given by Math.ceil(Math.log10(i)), for integral
i
.第一个解决方案:
第二个解决方案:
1st solution:
2nd solution:
提示:您需要将数字除以 10,以获得最后一位数字。然后将同一个数除以 10,得到前两个数。根据需要重复多次。
Hint: You need to take the modulus of the number by 10, to get the last digit. And then divide the same number by 10, do get the first two numbers. Repeat yourself as many times as required.