+ 5

Military Time Problem

My code fails on case 3 and 5, can anyone tell me what's wrong with the code string[] numin = Console.ReadLine().Split(':',' '); if (numin[2].Contains('P')) { int numout = Convert.ToInt32(numin[0]); numout += 12; string numouts = Convert.ToString(numin[1]); Console.Write(numout); Console.Write(':') Console.Write(numouts[0]); Console.Write(numouts[1]); } else if (numin[2].Contains('A')) { Console.Write(numin[0]); Console.Write(':'); Console.Write(numin[1]); };

15th Jan 2020, 7:40 PM
Flayer
Flayer - avatar
22 Answers
+ 10
OK. I missed one more.. If it single digit like 8:00 AM output should be like 08:00
15th Jan 2020, 8:49 PM
Jayakrishna 🇮🇳
+ 28
Notice: Single digits in left side like 8:00 00:00 am 00:00 pm 00:20 am 00:20 pm Your code must output right for these examples
17th Jan 2020, 8:53 AM
Mehdi Eslami
Mehdi Eslami - avatar
+ 8
Paste code in code playground save code and share your code link here. That will make easy to find out errors.. For input 12:20pm your output 24:20 it should be 12:20 only..
15th Jan 2020, 8:24 PM
Jayakrishna 🇮🇳
+ 4
If input is 12:10am you need output 0:10 as output... If 12:05pm then you have output 12:05 only... By with your code what you getting..?
15th Jan 2020, 7:49 PM
Jayakrishna 🇮🇳
+ 3
OK. Fine.. Well come...
15th Jan 2020, 9:17 PM
Jayakrishna 🇮🇳
+ 3
t = input() hour = 0 if t[1] == ':': hour = int(t[0]) else: hour = int(t[0:2]) if t[len(t)-2:] == 'PM': if hour < 12: hour += 12 elif t[len(t)-2:] == 'AM': if hour < 12: hour = hour else: pass total_time = '' if t[len(t)-2:] == 'PM': total_time = str(hour) + ':' + t[len(t)-5:len(t)-3] elif t[1] == ':' and t[len(t)-2:] == 'AM': total_time = '0' + str(hour) + ':' + t[len(t)-5:len(t)-3] elif t[2] == ':' and t[len(t)-2:] == 'AM' and t[0] == '0': total_time = '0' + str(hour) + ':' + t[len(t)-5:len(t)-3] elif t[2] == ':' and t[len(t)-2:] == 'AM': total_time = str(hour) + ':' + t[len(t)-5:len(t)-3] else : pass print(total_time)
27th Jun 2020, 12:39 PM
Pranit Bhoir
Pranit Bhoir - avatar
+ 2
Jayakrishna still nothing string[] numin = Console.ReadLine().Split(':',' '); if (numin[2].Contains('P')) { int numout = Convert.ToInt32(numin[0]); numout += 12; string numouts = Convert.ToString(numin[1]); Console.Write(numout); Console.Write(':'); Console.Write(numouts[0]); Console.Write(numouts[1]); } else if (numin[2].Contains('A')) { int bollout = Convert.ToInt32(numin[0]); if (bollout != 12) { Console.Write(numin[0]); Console.Write(':'); Console.Write(numin[1]); } else { Console.Write("00"); Console.Write(':'); Console.Write(numin[1]); } };
15th Jan 2020, 8:20 PM
Flayer
Flayer - avatar
+ 2
After numout value assigning change like this... And check again... if(numout!=12) numout += 12;
15th Jan 2020, 8:35 PM
Jayakrishna 🇮🇳
+ 2
It didn't work did it work for u?
15th Jan 2020, 8:41 PM
Flayer
Flayer - avatar
+ 2
Do you changed above one.? . I done it java. Let me see description.. What is error? .
15th Jan 2020, 8:43 PM
Jayakrishna 🇮🇳
+ 2
It's not an error it fails the cases
15th Jan 2020, 8:46 PM
Flayer
Flayer - avatar
15th Jan 2020, 9:08 PM
Flayer
Flayer - avatar
+ 2
Flayer i faced the same problem, The problem is if the input is of single digit then we need to print output with an additional 0, I.e. 6:45 AM Op - 0645 check this, https://www.sololearn.com/Discuss/2135879/?ref=app
16th Jan 2020, 4:44 PM
Akash
Akash - avatar
+ 2
Akash Sil I have already fixed it just do something like If input[0] < 10 write("0") before the actually result
16th Jan 2020, 4:47 PM
Flayer
Flayer - avatar
+ 1
There is an easier way to write this. Check it ;). https://code.sololearn.com/csvOritrsLMi
13th Mar 2020, 2:51 PM
Ambrozja 🇵🇱
Ambrozja 🇵🇱 - avatar
+ 1
DateTime is one of the things I hate the most in c# Jolanta Malinowska but thanks
13th Mar 2020, 10:57 PM
Flayer
Flayer - avatar
0
Hlo
16th Jan 2020, 6:29 PM
rajesh
rajesh - avatar
0
Hlw
17th Jan 2020, 11:20 AM
MohammadShôv
MohammadShôv - avatar
0
Jolanta Malinowska you have to send a playground link not a coach cuz I can't see it
13th Mar 2020, 5:12 PM
Flayer
Flayer - avatar