Help with parking fee (kotlin) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with parking fee (kotlin)

I am having issues trying to solve the parking fee question here is what I attempted to do fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 var one = 1.0 var pointfifty = 0.5 if (hours < 5){ hours = hours println(hours) } else if (hours > 5){ var totals = hours / 1 total =(one * 5) + (totals * pointfifty ) println(total) } } Any help is appreciated

15th Feb 2021, 11:27 PM
ipaz12
ipaz12 - avatar
5 Answers
+ 4
(1) The output is always Double, but in the if statement you print an int. (2) You didn't implement the parking fee which is 24 hours per $15. (3) You else algorithm is wrong, if hours > 5, it will be 5 from the first 5 hours plus the rest of hours in a day multiplied by 0.5.
16th Feb 2021, 1:57 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 7
This is mine. I hope this can help you fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 var day : Int = hours/24 var addhours : Int = hours%24 total = when { hours >5 && hours <24 -> 5.0+(hours-5)*0.5 hours >= 24 -> (addhours*0.5) + (day*15) else -> hours*1.0 } println(total) }
6th Mar 2021, 2:11 PM
M. Huda Saputra
M. Huda Saputra - avatar
+ 1
I tired several logics write the function in an unbuggy way... this is what I have written and it worked perfectly!! var hours = readLine()!!.toInt() var r = (hours / 24).toInt() var total: Double = 0.0 if(hours >= 24){ hours = hours - (r*24) total = total+(r*15) if(hours >=0){ total = total+(0.5*hours) } } else if(hours > 5 && hours < 24){ hours -= 5 total += 5 total = total+(0.5*hours) }else{ total = hours * 1.0 } println(total)
19th Feb 2021, 9:21 AM
Hussein Alaeddine
Hussein Alaeddine - avatar
+ 1
I tried the following on the IDE, it gives the correct results, but here on Sololearn ...although it says something like: Output: 11.5 Expected: 11.5 🤦 fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 total = when { hours == 24 -> 15 * 1.0 hours > 5 && hours < 24 -> 5 + (hours - 5) * 0.5 else -> hours * 1 } println(total) }
20th Feb 2021, 5:29 PM
Kelvin Mubanga
Kelvin Mubanga - avatar
0
This works fun main(args: Array<String>) { var hours = readLine()!!.toInt() var changed: Double = hours + 0.0 var total: Double = 0.0 var days: Int = hours/24 when { changed <= 5.0 -> total = changed * 1.0 changed >= 5.0 && changed <= 24.0 -> total = (changed - 5.0) * 0.5 + 5.0 changed >= 24.0 -> total = 15 * days + (hours %24) * 0.5 } println(total) }
6th Mar 2021, 8:18 AM
Eashan Morajkar
Eashan Morajkar - avatar