[SOLVED] Emm Wtf??? What's wrong?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[SOLVED] Emm Wtf??? What's wrong??

fun shippingCost(amount: Double, international: Boolean): Double { var x = 0 when { international == true && amount.toInt() <= 50 -> var x = amount.toInt() * 15 / 100 international == false && amount.toInt() < 75 -> var x = amount.toInt() * 10 / 100 international == false && amount.toInt() >= 75 -> var x = amount international == true && amount.toInt() > 50 -> println("Invalid") } return (amount + x).toDouble() } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) } It supose I must find the price of the orders, with a 10% for import add to total price, that's for < 75$ and 75+ is for free, so that's no international, in case to be international, the import is 15% with a max of 50$

24th Dec 2021, 4:52 AM
RichardVM
RichardVM - avatar
15 Answers
+ 4
Yes, this line: var x = 0 is a declaration for an integer! You have to use: var x = 0.0 That's a double type
24th Dec 2021, 2:57 PM
Coding Cat
Coding Cat - avatar
+ 5
Coding Santa Thanks you so much for your help bro 👍👍👍 it's alright, but there's one case wrong, and i dont have any idea 🤣🤣 but don't worry I'll figure it out, thanks you so much 👌👍
24th Dec 2021, 3:10 PM
RichardVM
RichardVM - avatar
+ 3
Oooooooo 🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️
24th Dec 2021, 3:04 PM
RichardVM
RichardVM - avatar
+ 2
You should not calculate with Integer and convert the return value to double. This way you will lose the dezimals. And you are using much code for this. As a hint, you can have look at this (AFTER your version is working) https://code.sololearn.com/c6f3aa6fQAIX/?ref=app
24th Dec 2021, 11:06 AM
Coding Cat
Coding Cat - avatar
+ 2
Your func declaration is: fun shippingCost(amount: Double,.... That's ok. But then you are always using: amount.toInt() or a.toInt() for your calculation. This way, the following will be happen: If the input for amount was originaly 23.45 you make 23 from that and calculate with this wrong value. The dezimals are cutted off. And even if you convert the result to double, it's wrong.
24th Dec 2021, 2:03 PM
Coding Cat
Coding Cat - avatar
+ 2
Yes, Agnes Ahlberg is right. One more hint: There is no need to convert anything in the whole code. You starts with double and boolean at the input. Use this for any calculation and return / print a double. And the correct multiplier for 15% is 0.15. Because we are only intrested on the shipping cost portion.
24th Dec 2021, 2:30 PM
Coding Cat
Coding Cat - avatar
+ 2
Thanks you so much Coding Santa I'll try it man
24th Dec 2021, 3:04 PM
RichardVM
RichardVM - avatar
+ 1
fun shippingCost(a: Double, i: Boolean): Double { var x = 0 when { i == false && (a.toInt()) < 75 -> x = ((a.toInt())*10/100) i == false && (a.toInt()) >= 75 -> x = 0 else -> x = a.toInt()*15/100 } return x.toDouble(); } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) } I fix it a little, but I don't understand why it say error, I mean, I use a calculator, and when you find 15% of 199.9 is 29.985, but when the Code Playground does, it say 29, so I don't know what's happening :(
24th Dec 2021, 5:42 AM
RichardVM
RichardVM - avatar
+ 1
Some languages use integer division if only integers are involved 🤓 Try making something a Double, like 15.0/100, or something language appropriate 🍀🙂
24th Dec 2021, 7:40 AM
Agnes Ahlberg
Agnes Ahlberg - avatar
+ 1
It's just the Code Playground says error when I try to use the Double to the calc, i set it like this: a*15.0/100.0 but it says that an Int was expected 😐
24th Dec 2021, 1:49 PM
RichardVM
RichardVM - avatar
+ 1
You have to watch out for types. A Double is not an Int. If you do: var x = 0 Then x is of type Int. Then you cannot assign Double in the when. When you do: var x = 0.0 Then x is of type Double. Then you cannot assign x = 0 in the when block. Check that all your types are correct and match throughout 🙂 I will leave it to Coding Santa now to help you out so that things don't get messy with too many cooks 🙂🍀
24th Dec 2021, 2:12 PM
Agnes Ahlberg
Agnes Ahlberg - avatar
+ 1
Ok... Coding Santa Agnes Ahlberg I'll try with 0.15
24th Dec 2021, 2:44 PM
RichardVM
RichardVM - avatar
+ 1
Coding Santa I don't know what to do man 🥲 look at this: fun shippingCost(a: Double, i: Boolean): Double { var x = 0 when { i == false && a < 75.0 -> x = a*0.10 i == false && a >= 75.0 -> x = a*0.0 else -> x = a*0.15 } return x } But it don't works, it says the same thing, that Int was expected 😐 They want i calcule with Int but then I'll lose the decimals so the result will be wrong 😐 pls help me and thanks for be pacient 🤣 I'm to slow for learn
24th Dec 2021, 2:51 PM
RichardVM
RichardVM - avatar
0
Coding Santa Creo que ví x ahí algo de multiplicar x 1.15, que me dices? I mean, I could use that? (i don't remember where I saw it)
24th Dec 2021, 1:39 PM
RichardVM
RichardVM - avatar
0
I tried to use another variable, that use amount as a Integer, but it don't works :'(
24th Dec 2021, 2:52 PM
RichardVM
RichardVM - avatar