+ 1

Whats wrong in this code?

fun main(args: Array<String>) { var hours = readLine()!!.toInt() if(hours == 5) { println(5) } else if(hours > 5 && hours < 24 ){ var a = hours - 5; print((a *0.5) + 5) } else if(hours > 24){ var a = hours - 24 println((a * 0.5) + 15) } else{ println(15) } }

1st Mar 2021, 7:01 AM
Darshan Pagar
Darshan Pagar - avatar
6 Answers
+ 5
It's else if not if else. You need to multiply hours by 1.0 in your else statement because total is Double
1st Mar 2021, 8:42 AM
Simba
Simba - avatar
+ 4
Darshan Pagar Hours maybe more than 48 also and for each 24 hours, $15 will be pay so here you have to do if (hours > 24) { total = (hours / 24) * 15 + (hours % 24) * 0.5; }
1st Mar 2021, 7:20 AM
A͢J
A͢J - avatar
+ 4
This is my code that pass all test. Please check it . fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours <= 0) total = 0.0; else if(hours <= 5) total = hours * 1.0 else if(hours > 5 && hours < 24) total = 5 + ((hours - 5) * 0.5) else if(hours == 24) total = 15.0 else if(hours > 24) total = ((hours / 24) * 15) + ((hours % 24) * 0.5); print(total) }
1st Mar 2021, 11:45 AM
Wei Phyo Aung
Wei Phyo Aung - avatar
+ 1
Simba No need to write 1.0. 1 will also work.
1st Mar 2021, 8:51 AM
A͢J
A͢J - avatar
+ 1
Darshan Pagar Look at this 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) }
2nd Mar 2021, 6:03 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
0
fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours > 24){ total = (hours/24)*15 + (hours%24)*0.5 println(total) } if else(hours > 5 && hours < 24){ total = (hours - 5)*0.5 + 5 println(total) } else{ total = hours*1 println(total) } } This is my final code , but some error is coming. Saying expecting parentheses
1st Mar 2021, 8:11 AM
Darshan Pagar
Darshan Pagar - avatar