+ 1

Please who can help me solve this/tell me what's wrong with my code

Description: You are making a car parking software that needs to calculate and output the amount due based on the number of hours the car was parked. The fee is calculated based on the following price structure: - the first 5 hours are billed at $1 per hour. - after that, each hour is billed at $0.5 per hour. - for each 24 hours, there is a flat fee of $15. This means, that, for example, if a car parked for 26 hours, the bill should be 15+(2*0.5) = 16.0, because it was parked for 24 hours plus 2 additional hours. Sample Input: 8 Sample Output: 6.5 attempt: fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if( hours > 5 && hours <24){ total += 0.5 hours = 24 } else if(hours > 24){ total += 15 hours 24 } else{ total +=1; hours - 5; } println(total) }

22nd Feb 2021, 11:01 PM
Code_wizard💯🔥
Code_wizard💯🔥 - avatar
2 odpowiedzi
+ 3
Code_wizard💯🔥 You are on the right track. There is no need to manipulate the hours variable. First if hours is less than or equal to 5 then then total should be hours * 1.0 Otherwise if hours is less than 24 then total should be 5.0 + ((hours -5) * 0.5) Otherwise total should be hours / 24 * 15.0 + (hours % 24 * 0.5) Using an if / else if / else as you have done adjusted with this logic will fix your code.
23rd Feb 2021, 3:43 AM
Paul K Sadler
Paul K Sadler - avatar
+ 1
work through the code with an example value line by line, and pretend that you are the interpreter. That might lead you to the answer.
23rd Feb 2021, 3:34 AM
Wilbur Jaywright
Wilbur Jaywright - avatar