+ 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

9th Apr 2021, 2:48 PM
JustWrecker !
JustWrecker ! - avatar
3 Answers
+ 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.
9th Apr 2021, 2:54 PM
ä½ ēŸ„é“č¦å‰‡ļ¼Œęˆ‘也ę˜Æ
ä½ ēŸ„é“č¦å‰‡ļ¼Œęˆ‘也ę˜Æ - avatar
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) } }
19th Jan 2022, 9:18 PM
Alharith Albayati
0
The Forth hidden test input seems x <=5 We must print the total as Double. I print(hours) and continued to fail.
12th Apr 2022, 9:20 AM
makoto tanaka
makoto tanaka - avatar