0
I do not know why the test four of my Parking fee program is not running. Can I get a hand please?!
fun main(args: Array<String>){ var hours = readLine()!!.toInt() var total: Double=0.0 val differenceOne=(hours-24) val differenceTwo=(hours-5) val flatfee=15 val mod=hours-(hours%24) val divid=mod/24 val differenceThree=(hours%24) if (hours==1 && hours<=5){ println(hours*1) } else if(hours%24==0){ println(flatfee*divid) } else if(hours>=5 && hours<24){ print(5+(differenceTwo*0.5)) } else if(hours>24 && hours<48){ println((flatfee)+(differenceOne*0.5)) } else if(hours>24 && hours%24!=0){ println((flatfee*divid)+(differenceThree*0.5)) } else{ println(hours) } }
6 odpowiedzi
+ 6
if (hours >=1 && hours<=5){
println(hours*1.0)
}
+ 1
Actually you're right.
Thanks.
+ 1
Thanks Rachael Loveth, i’m gonna upgrade my code
0
Still the same thing.
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if(hours in 0..5){
total=hours*1.0
}
else if(hours in 6 until 24){
total=5.0+((hours-5.0)*0.5)
}
else if(hours>24){
val x=hours/24
total=(15.0*x)+(hours-(x*24))*0.5
}
else{
total=0.0
}
println(total)
}
0
Rachael Loveth Alex Ivan
Only as a hint: Keep in mind that you also can use a simple "when" statement. And you can print the result directly. There is no need for any additionel variable.
With some more experience you will do it like this:
fun main(args: Array<String>) {
var h = readLine()!!.toInt()
println ( when {
h < 6 -> h*1.0
h < 24 -> 5+(h-5)*0.5
else -> 15*(h/24) + 0.5*(h%24)
} )
}