我正在尝试对Hackerrank进行Time Conversion挑战,但出于某种原因,24小时制表示中的小时字段总是空出来。这是挑战 -
问题陈述
给定AM / PM格式的时间,将其转换为军事(24小时)时间。
注意:午夜时间是12:00 AM上午12:00:00和上午00:00:00 24小时制。中午时间是12:00 PM,时间是12:00:00 24小时制。
另一个问题似乎是秒始终显示为1位数。例如,如果输入为07:05:45PM
,则我的Hackerrank输出为:05:4
。
但是,代码在桌面上的IntelliJ中运行得很好 -
1:24:23AM
1
24
23AM
01:24:23
Process finished with exit code 0
和
07:05:45PM
07
05
45PM
19:05:45
Process finished with exit code 0
由于没有办法在hackerrank本身上调试解决方案,我不确定是什么问题。这是我的代码 -
package algorithms.Warmup;
import java.util.Scanner;
/**
* Created by manishgiri on 1/6/16.
*/
public class TimeConversion {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String time = in.next();
String[] parts = time.split(":");
for(String part: parts){
System.out.println(part);
}
int hours = Integer.parseInt(parts[0]);
String minutes = (parts[1]);
String[] last = parts[2].split("");
String seconds = last[0]+last[1];
String timeZ = last[2]+last[3];
String finalHour = "";
if(timeZ.equalsIgnoreCase("PM")) {
if(hours == 12) {
finalHour = Integer.toString(12);
}
else {
finalHour = Integer.toString(hours + 12);
}
}
else if(timeZ.equalsIgnoreCase("AM")) {
if(hours == 12) {
finalHour = "00";
}
else if(hours == 10 || hours == 11) {
finalHour = Integer.toString(hours);
}
else {
finalHour = "0"+hours;
}
}
System.out.println(finalHour+":"+minutes+":"+seconds);
}
}
答案 0 :(得分:1)
尝试以下代码。希望这些评论有助于理解逻辑。
static String timeConversion(String s) {
// Split the string by :
String[] sTime = s.split(":");
int x = 0;
// if PM and hours >12, add additional 12 to hours
// for AM and hour = 12, set hour to 00
if(sTime[sTime.length - 1].contains("PM") && !sTime[0].equals("12"))
x = 12;
String val1 = "";
if(x == 12)
val1 = (Integer.parseInt(sTime[0]) + x) + "";
else {
if(sTime[0].equals("12") && sTime[sTime.length - 1].contains("AM"))
val1 = "00";
else
val1 = sTime[0];
}
// merge the string and return the result
String result = val1 + ":" + sTime[1] + ":" + sTime[2].substring(0,2);
return result;
}
干杯!
答案 1 :(得分:0)
我不知道为什么您的JRE环境和Hackerrank环境之间存在不一致。但是如果我们提出你的问题,在hackerrank你的代码将part [2]分成5个部分,导致空字符串。
这意味着如果您更新
String seconds = last[0]+last[1];
String timeZ = last[2]+last[3];
作为
String seconds = last[1]+last[2];
String timeZ = last[3]+last[4];
它应该运行,
并且为了防止初始空字符串,您可以在下面找到一些建议:
How to prevent java.lang.String.split() from creating a leading empty string?
答案 2 :(得分:0)
您可以尝试使用hackerrank -
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String time = in.next();
int hours;
String hrs = time.substring(0,2);
hours = Integer.parseInt(hrs);
String mins = time.substring(3,5);
String secs = time.substring(6,8);
String am_pm = time.substring(time.length()-2,time.length());
if(am_pm.equalsIgnoreCase("PM") && !hrs.equalsIgnoreCase("12")){
hours += 12;
hrs = Integer.toString(hours);
}
else if(am_pm.equalsIgnoreCase("AM") && hrs.equalsIgnoreCase("12")){
hours = 0;
hrs = "00";
}
System.out.println(hrs+":"+mins+":"+secs);
}
答案 3 :(得分:0)
你可以试试这个..
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String time = in.next();
String[] sTime = time.split(":");
int x = sTime[sTime.length - 1].contains("PM") ? 12 : 0 ;
String val1 = (x == 12) ? (Integer.parseInt(sTime[0]) + x) + "" : (sTime[0].equals("12") ? "00" : sTime[0]);
String result = val1 + ":" + sTime[1] + ":" + sTime[2].substring(0,2);
System.out.println(result);
}
答案 4 :(得分:0)
public static String timecon(String s) {
System.out.println(s.substring(0, 2));
int test = Integer.valueOf(s.substring(0, 2));
if ((s.substring(0, 2).equals("12")) && s.substring(8, 10).equals("AM")) {
return "00" + ":" + s.substring(3, 8);
} else if (s.substring(0, 2).equals("12") && (s.substring(8, 10).equals("PM"))) {
return s.substring(0, 8);
} else if (s.substring(8, 10).equals("AM")) {
return s.substring(0, 8);
}
else {
return test + 12 + ":" + s.substring(3, 8);
}
}
答案 5 :(得分:0)
您可以在Java中使用此工具进行时间转换Hackerrank
/*
* Complete the timeConversion function below.
*/
static String timeConversion(String s) {
String h,m,sec;
h = s.substring(0,2);
m = s.substring(3,5);
sec = s.substring(6,8);
if(s.contains("PM") && !h.equals("12")){
int inc = 12;
inc += Integer.parseInt(h);
h = inc + "";
}
if(s.contains("AM") && h.equals("12")){
h = "00";
}
String milTime = h + ":" + m + ":" + sec;
return milTime;
}
答案 6 :(得分:0)
试试这个:
public static String timeConversion(String s) {
int h = Integer.parseInt(s.substring(0,2));
if (h == 12) h = 0;
if (s.substring(8,9).equals("P")) h = h + 12;
return String.format("%02d", h) + s.substring(2,8);
}
答案 7 :(得分:-1)
def timeConversion(s):
# time_r=s[:-2]
am_pm=s[-2:]
if am_pm=='PM' and s[:2]!='12':
s=str(int(s[:2])+12) + s[2:]
if am_pm=='AM' and s[:2]=='12':
s="00" + s[2:]
return s[:-2]
print(timeConversion("07:05:45PM"))