标签:lse git practice maximum sse max maker write put
1.遇到这么个题目
Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)
HH = hours, padded to 2 digits, range: 00 - 99
MM = minutes, padded to 2 digits, range: 00 - 59
SS = seconds, padded to 2 digits, range: 00 - 59
The maximum time never exceeds 359999 (99:59:59)
测试代码如下
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExampleTest {
@Test
public void Tests() {
assertEquals("makeReadable(0)", "00:00:00", HumanReadableTime.makeReadable(0));
assertEquals("makeReadable(5)", "00:00:05", HumanReadableTime.makeReadable(5));
assertEquals("makeReadable(60)", "00:01:00", HumanReadableTime.makeReadable(60));
assertEquals("makeReadable(86399)", "23:59:59", HumanReadableTime.makeReadable(86399));
assertEquals("makeReadable(359999)", "99:59:59", HumanReadableTime.makeReadable(359999));
}
}
于是,傻乎乎的写下了这么多代码
package com.practice.JUnit;
public class HumanReadableTime {
public static String makeReadable(int seconds) {
// Do something
int hour = 0;
int minute = 0;
int second = 0;
if(seconds < 60){
hour = 0;
minute = 0;
second = seconds;
}else if(seconds < 3600){
hour = 0;
minute = seconds / 60;
second = seconds - minute * 60;
}else if(seconds >= 3600){
hour = seconds / 3600;
minute = (seconds - hour * 3600)/60;
second = (seconds - hour * 3600) - minute * 60;
}
String sst = null;
String mst = null;
String hst = null;
if(second < 9) sst = "0" + second;
else sst = second + "";
if(minute < 9) mst = "0" + minute;
else mst = minute + "";
if(hour < 9) hst = "0" + hour;
else hst = hour + "";
String s = hst + ":" + mst + ":" + sst;
return s;
}
}
答案是:
public class HumanReadableTime {
public static String makeReadable(int seconds) {
return String.format("%02d:%02d:%02d", seconds / 3600, (seconds / 60) % 60, seconds % 60);
}
}
(╬◣д◢)哦!万能的API
标签:lse git practice maximum sse max maker write put
原文地址:https://www.cnblogs.com/miaowulj/p/12203574.html