Java将任意整数转换为4位数字
这似乎是一个简单的问题。我的一项作业基本上是以军事格式(例如 1200
、2200
等)向我的班级发送时间。
当我的班级收到整数时,如何强制将其转换为 4 位数字?例如,如果发送的时间为 300
,则应将其转换为 0300
。
编辑:事实证明我不需要这个来解决我的问题,因为我只需要比较这些值。谢谢
This seems like an easy question. One of my assignments basically sends a time in military format (like 1200
, 2200
, etc) to my class.
How can I force the integer to be converted to 4 digits when it's received by my class? For example if the time being sent is 300
, it should be converted to 0300
.
EDIT: it turns out i didnt need this for my problem as i just had to compare the values. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就这么简单:
为了比较小时和分钟:
注意:
Integer.compare(i1, i2)
已在 Java 1.7 中添加,对于以前的版本,您可以使用Integer.valueOf(i1 ).compareTo(i2)
或As simple as that:
For comparing hours before minutes:
Note:
Integer.compare(i1, i2)
has been added in Java 1.7, for previous version you can either useInteger.valueOf(i1).compareTo(i2)
or