+ 1
Military Time in Ruby
My code: a=gets b=a[0..4] ans="" if a[-2]=="P" if b[1]==":" ans=(b[0])+12+b[1...] else if b[0...2]== "12" ans="12"+b[2...] else ans=((b[0..2]).to_i+12).to_s+b[2...] end end else if b[1]==":" ans="0"+b[0...] else if b[0..2]=="12" ans="00"+b[2...] else ans=b end end end print(ans) Keep getting the #4 case wrong. Does anybody have any ideas?
4 Réponses
+ 1
Hi! your program does not work with the value 1: 15 PM
0
Military Time in Ruby
My code:
a=gets
b=a[0..4]
ans=""
if a[-2]=="P"
if b[1]==":"
ans=((b[0]).to_i+12).to_s+b[1...]
else if b[0...2]== "12"
ans="12"+b[2...]
else
ans=((b[0..2]).to_i+12).to_s+b[2...]
end
end
else
if b[1]==":"
ans="0"+b[0...]
else if b[0..2]=="12"
ans="00"+b[2...]
else
ans=b
end
end
end
print(ans)
0
Good! This code pass all tests!
0
input = gets.chomp
if input.include?("AM")
if input[1] == ":"
time = "0" + input[0] + ":" + input[2..3]
else
time = input[0..1] + ":"+ input[3..4]
end
else
if input[1] == ":"
time = (input[0].to_i + 12).to_s + ":" + input[2..3]
else
time = (input[0..1].to_i + 12).to_s + ":"+ input[3..4]
end
end
puts time