+ 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) } }
6 Réponses
+ 5
It's else if not if else.
You need to multiply hours by 1.0 in your else statement because total is Double
+ 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;
}
+ 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)
}
+ 1
Simba
No need to write 1.0. 1 will also work.
+ 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)
}
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