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

+1

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 !

3 Answers

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.

0

Try this code out :- fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours<=5){ total += hours println(total) } else if(hours>5&&hours<24){ total += 5 + (hours - 5) * 0.5 println(total) } else if(hours==24){ total += 15 println(total) } else if(hours>24){ total += (hours/24)*15+(hours%24)*0.5 println(total) } }

0

The Forth hidden test input seems x <=5 We must print the total as Double. I print(hours) and continued to fail.