+ 5
Where the error?
You are making a car parking software that needs to calculate and output the amount due based on the number of hours the car was parked. The fee is calculated based on the following price structure: - the first 5 hours are billed at $1 per hour. - after that, each hour is billed at $0.5 per hour. - for each 24 hours, there is a flat fee of $15. Code: fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours in 1..5) total=hours*1.0 else if(hours>5&&hours<24) total=5+((hours-5)*0.2) else total=15+(2*0.5) print(total) }
7 ответов
+ 11
In your 2nd condition total should be like this
total = 5+((hours-5)*0.5)
If hours is 50, it should be
total = 50/24*15+(50%24)*0.5
+ 5
Thanks it worked but can you explain it
total=hours/24*15+(hours%24)*0.5
Simba
+ 5
If the guy parks for 2 or more full days you will need to calculate each 24 hours and multiply that by 15$
That's why you get the "hours/24*15" section.
Then you need to get the amount of hours that remains unaccounted for using the % operator and multiply that by 0.5$ that gets you to "(hours%24)*0.5"
+ 4
JavaidIqbal
This is my code,
Hope maybe helps you:
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if (hours > 24) {
total += 15 * (hours / 24) + 0.5 * (hours % 24)
} else if(hours < 24 && hours > 5) {
total += (hours - 5) * 0.5 + 5
} else {
total += hours * 1
}
println(total)
}
+ 3
How about if someone parks for 2 days or longer?
You need to calculate out all the days for the 24 hour rate. Then 0.5 for the balance.
If someone parked for 50 hours. That would be 2 days and 2 hours.
That's why we have to calculate total in this way :)
+ 3
Abd-ElRahman Abdeen
Thanks!
+ 1
Welcome ☺️☺️