0

# Parking fee where I am getting wrong

fun main(args: Array<String>) { var x = readLine()!!.toInt() var y : Double = 0.5 when { x <= 5 -> println( x*1 ) x > 5 && x < 24 -> println(2.5+(x*y)) x > 24 -> println( (x%24)*y + (x/24)*15) } }

2 Answers

+ 4

woney
2 little mistakes
1 was logic error, the other syntax.
Your code:
fun main(args: Array<String>) {
var x = readLine()!!.toInt()
var y : Double = 0.5
when {
x <= 5 -> println( x*1.0 )
x > 5 && x < 24 -> println(5+(x-5)*y)
x > 24 -> println( (x%24)*y + (x/24)*15)
}
}

+ 1

I think it has something to do with the case x > 24
You’re taking the modulus of total hours and 24, multiplied by 0.5 (correct), but then adding it to total hours divided by 24 times 15. This wont always resolve to a whole number.
Ex: 30 hours (expect $18)
In your code:
x%24 = 6
6*0.5 = 3
x/24 = 30/24 = 5/4
5/4 * 15 = 75/4 = 18.75
3 + 18.75 = 21.75
You’ll want to round x/24 down to nearest integer (i think Kotlin has a Math.Floor function??) before multiplying by 15.