Kotlin Shipping Calculator | Sololearn: Learn to code for FREE!

0

Kotlin Shipping Calculator

I am having trouble producing an output in the terminal. Can someone tell me what is wrong with my code? The parameters are that orders that are over $75 have free shipping. Orders less than $75 have a 10% fee to the total amount. There is an international fee of 15% with the maximum fee of $50. The two inputs are a double and bolean. Where I am having trouble is the output. I do not know what is wrong with the code; in the terminal it says "no output." Please help. fun main(args: Array<String>) { fun shippingCost(amount: Double, international: Boolean): Double { if (international == false && amount > 75) { println(amount + 0.0) } else if (international == false && amount < 75) { return (amount * 0.10) } else if (international == true && amount < 333.3334) { return (amount * 0.15) } else { return (amount + 50.0) } val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) return(shippingCost(total, international)) } }

4/29/2021 6:22:10 AM

nathon c

6 Answers

New Answer

+3

You need to add additional closing curly bracket to close the shippingCost method before reading the values. Also, inside the first if statement there should be return instead of println. Remove the extra closing bracket at the end of your code. This should get rid of errors, but your calculations are a bit off still.

+2

Ejeh Wayne I never responded because Aleksandrs had already provided a suitable answer to the problem.

+2

solution with less code - fun shippingCost(amount: Double, international: Boolean): Double { return when{ amount>75 && !international -> 0.0 amount<75 && !international -> amount*0.1 (amount*0.15>50) && international -> 50.0 else -> amount*0.15 } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }

+1

Hi nathon c I would like to test your code a bit to see if I can help, but can't get access to playground due to the current glitch. When it returns, I will see if I can help you resolve this problem.

+1

Rik Wittkopp hasnt it return?...its been four months now

+1

1) Why you have a return statement at the bottom of the main function? it's completely unnecessary 2) Even with removing it, I get errors running your code that the code is unreachable 3) Why comparing amount to 333.3334? that's really a difficult way of doing this. The whole code need to be restructured. Please have a look here: https://code.sololearn.com/kotlin