我是新手,目前正在尝试制作一些Timer应用,但我难以在时间上添加零(因此显示01:00而不是1:0)
我尝试使用&&,但是它给我的结果与两个If语句不同。
if(minutes <= 9){
minutesString = "0" + minutesString; }
if(seconds <= 9){
secondsString = "0" + secondsString; }
// This code works and shows my time with zeros (01:00)
if(seconds <= 9 && minutes <= 9){
secondsString = "0" + secondsString;
minutesString = "0" + minutesString; }
// This code doesnt work, it only has zero on minute till seconds are
//less than 9, after that minuts are single digit
答案 0 :(得分:1)
例如,Java具有执行零填充String
格式的功能;适当命名的String.format
可以为您完成此操作。喜欢,
int minutes = 0, seconds = 0;
String timerString = String.format("%02d:%02d", minutes, seconds);
System.out.println(timerString);
填充为零的上述输出
00:00