Parking fee problem (kotlin) | Sololearn: Learn to code for FREE!

0

Parking fee problem (kotlin)

I don't understand why doesn't this code pass the fourth hidden test fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 var days = hours/24 if (hours in 1..5) total = hours * 1.0 if(hours in 6..23) total= 5+(hours-5)*0.5 else total=days*15+(hours%24)*0.5 println(total) } But if I write "else if" instead of the second "if" it passes all the tests

4/9/2021 2:48:06 PM

JustWrecker !

1 Answer

New Answer

+3

The else statement is grouped with the second if statement. That means if the second if statement is false, else block will be executed. Note that it doesn't related to the first if statement. If hours is 4, both first if block and else block are executed.