+ 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") } }

23rd Dec 2021, 5:23 PM
RichardVM
RichardVM - avatar
23 odpowiedzi
+ 2
Yeah you had to us the modulus sign in order to get the extra time then proceed with the calculations
23rd Dec 2021, 6:59 PM
Ongora David Otieno
+ 4
Coding Santa You're so good man 💪💪 now i know how it works i understand it better, thank you very much 👍
23rd Dec 2021, 6:58 PM
RichardVM
RichardVM - avatar
+ 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) } ) }
23rd Dec 2021, 6:56 PM
Coding Cat
Coding Cat - avatar
+ 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)
23rd Dec 2021, 6:13 PM
Coding Cat
Coding Cat - avatar
+ 2
Coding Santa I'll try it very thanks bro! 🙏🙏
23rd Dec 2021, 6:50 PM
RichardVM
RichardVM - avatar
+ 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) }
23rd Dec 2021, 6:50 PM
Ongora David Otieno
+ 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 😅)
23rd Dec 2021, 6:54 PM
RichardVM
RichardVM - avatar
+ 1
RichardVM you are welcome 🙋‍♂️
23rd Dec 2021, 7:01 PM
Coding Cat
Coding Cat - avatar
0
visit my profile then cross-reference the code on the same
23rd Dec 2021, 5:53 PM
Ongora David Otieno
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.
23rd Dec 2021, 6:25 PM
Coding Cat
Coding Cat - avatar
0
Thanks for the compliment 😎
23rd Dec 2021, 6:27 PM
Ongora David Otieno
0
Coding Santa Have u found it helpful ?
23rd Dec 2021, 6:28 PM
Ongora David Otieno
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.
23rd Dec 2021, 6:36 PM
Coding Cat
Coding Cat - avatar
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
23rd Dec 2021, 6:38 PM
RichardVM
RichardVM - avatar
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
23rd Dec 2021, 6:42 PM
Coding Cat
Coding Cat - avatar
0
Can i speak spanish? Coding Santa
23rd Dec 2021, 6:44 PM
RichardVM
RichardVM - avatar
0
Oh no. German maybe? I give u an example...
23rd Dec 2021, 6:46 PM
Coding Cat
Coding Cat - avatar
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)
23rd Dec 2021, 6:46 PM
William Owens
William Owens - avatar
0
Coding Santa With that sintaxis, i convert 42h to 24.0$ succesfully, so i don't see what i most change 😐
23rd Dec 2021, 6:48 PM
RichardVM
RichardVM - avatar
0
Hours = 50 = 2 full days => 2 x 15 plus 2 hours => 2 x 0.5 Sum = 31 €
23rd Dec 2021, 6:48 PM
Coding Cat
Coding Cat - avatar