0
Kotlin Parking fee project
can anyone help me evaluate this code because I am having issues with test case 4 in the kotlin project. fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 val hourChecker: Int = hours / 24 if (hours < 0){ println("Wrong Input. Please try again!") } else{ if (hours >= 5 && hours < 24) { total += 5 hours -= 5 } if (hours >= 24) { total = (15 * hourChecker) + (hours - (24 * hourChecker)) * 0.5 println(total) } else if (hours < 24) { total = total + (0.5 * hours) println(total) } else { total = hours * 0.5 println(total) } } }
4 Answers
+ 6
If hours < 5, total should be hours * 1.0
Also you need to change your else if condition
else if (hours > 5 && hours < 24) {
total = total + (0.5 * hours)
println(total)
}
else {
total = hours * 1.0
println(total)
}
+ 2
Copy this answer and please check and like thanks
+ 1
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)
}
0
I will post my full code here, I hope it helps, let me know if you have questions:
======
fun main(args: Array<String>) {
var hours = readLine()!!.toDouble()
var total: Double = 0.0
val days: Double
val remaining_hours: Double
if (hours >= 24) {
days = Math.floor(hours/24)
remaining_hours=hours - (days*24)
total= days*15 + (remaining_hours*.5)
}
else{
if (hours < 6){
total = hours
}
else{
total = 5+((hours-5)*.5)
}
}
println(total)
}
======