+ 1
It don't work!!! Help pls!! 🤧 I'm studyng kotlin, this is the Parking Price coding test, i'm stuck, why it don't works?[SOLVED]
fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if (hours <= 5) { println(hours.toDouble()*1) } else if (hours > 5 && hours < 24) { println(((hours.toDouble()) - 5) * 0.5) } else if (hours == 24) { println(((hours.toDouble())-24)*0.5 + 15) } else { println("invalid") } }
23 odpowiedzi
+ 2
Yeah you had to us the modulus sign in order to get the extra time then proceed with the calculations
+ 4
Coding Santa You're so good man 💪💪 now i know how it works i understand it better, thank you very much 👍
+ 3
And if you want to make it a little bit more Kotlin, you can do something 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)
} )
}
+ 2
Your 3. case (hours == 24) is wrong. You have to consider hours >= 24. That means all durations greater or equal 24 hours. (maybe longer than 1 or 2 or more days)
+ 2
Coding Santa I'll try it very thanks bro! 🙏🙏
+ 1
//Your code should simply be like this one bellow
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if (hours >= 24) {
total = ((hours / 24) * 15.0) + ((hours % 24) * 0.5)
}
else if (hours <= 5){
total = hours * 1.0
}
else {
total = 5.0 + ((hours - 5) * 0.5)
}
println(total)
}
+ 1
Thanks all of you guys 🙏🙏 Ongora David Otieno Coding Santa William Owens I just had to use % (i don't have any practice with it 😅)
+ 1
RichardVM you are welcome 🙋♂️
0
visit my profile then cross-reference the code on the same
0
Ongora David Otieno your example is very nice. But it mostly looks like Python style. You can do that a little bit more Kotlin like, with only the half of lines.
0
Thanks for the compliment 😎
0
Coding Santa Have u found it helpful ?
0
Ongora David Otieno
for the most beginner here it could be helpfull. Because it works. And the style looks like the same as my first steps in Kotlin. But meanwhile I've made a better / shorter version.
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if (hours <= 5) {
total = hours.toDouble()*1
}
else if (hours > 5 && hours < 24) {
total = (hours.toDouble() - 5) * 0.5 + 5;
}
else if (hours == 24) {
total = 15.0
}
else if (hours > 24) {
total = 15+(hours.toDouble() -24)*0.5
}
println(total.toDouble())
}
What's wrong? 😐
Coding Santa It says error in 3 of 6 test, pls help
0
You can't only simple do it with - 24
You have to find out the number of FULL days (24 hours) , multiplied by 15. AND add the hours over the FULL days multiplied by 0.5
0
Can i speak spanish? Coding Santa
0
Oh no. German maybe?
I give u an example...
0
IMHO: When statements look a little cleaner. Would this work?
val calc = when(){
hours <= 5 -> hours.toDouble()
hours < 24 -> ((hours.toDouble()) - 5) * 0.5
hours == 24 -> ((hours.toDouble())-24)*0.5 + 15
else -> "Invalid"
}
println(calc)
0
Coding Santa With that sintaxis, i convert 42h to 24.0$ succesfully, so i don't see what i most change 😐
0
Hours = 50
= 2 full days => 2 x 15
plus 2 hours => 2 x 0.5
Sum = 31 €