+ 1
Parking Fee Problem
I am wondering what's wrong with my code. No syntax errors as well. But it is showing 'No Output' for this code. It will be good if someone could help me out fix this. CODE:- ```fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours<=5) { total=hours println(total) } else if(hours>5&&hours<24) { total=5+((hours-5)*0.5) println(total) } else if(hours>=24) { total=15*(hours/24)+((hours-24)*0.5) println(total) } } ```
7 Answers
+ 3
Vishnuvasan Srinivasan
Here is my solution :
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)
}
+ 3
Vishnuvasan Srinivasan
You are getting no output because there is an error.
hours is an integer and total is double so you can't directly assign integer to double. To resolve this issue just do like this
total= hours * 1.0
+ 2
Vishnuvasan Srinivasan
There should be % not - in case of 24 because suppose there is 49 hours and you have to pay 15 / hours so there will be 24 * 15 + (49 % 24) * 0.5 but if you use - then it would be 24 * 15 + (49 - 24) * 0.5 which is wrong.
So here is right solution
total = 15 * (hours / 24) + (hours % 24) * 0.5
+ 2
I try to make as simple as i can.
This is mine
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if (hours <= 5){
total = hours * 1.0
}
else if (hours > 5 && hours <24){
total = 5.0+((hours-5.0)*0.5)
}
else{
total = ((hours/24)*15.0) + ((hours%24.0)*0.5)
}
println(total)
}
0
You can follow this approach :-
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
var take:Double
total = when{
hours<=0 -> 0.0
hours<=5 -> hours*1.0
hours>5 && hours<24 -> hours*1.0 - (hours-5)*0.5
hours>=24 -> (hours/24*15) + (hours%24*0.5)
else -> 0.0
}
println(total)
}
0
fun main(args: Array<String>) {
var x = readLine()!!.toInt()
var y : Double = 0.5
when {
x <= 5 -> println( x*1 )
x > 5 && x < 24 -> println(2.5+(x*y))
x > 24 -> println( (x%24)*y + (x/24)*15)
}
}
0
Try this code out:-
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if(hours<=5){
total += hours
println(total)
}
else if(hours>5&&hours<24){
total += 5 + (hours - 5) * 0.5
println(total)
}
else if(hours==24){
total += 15
println(total)
}
else if(hours>24){
total += (hours/24)*15+(hours%24)*0.5
println(total)
}
}