+ 1
[Solved] Q. Can someone help in shipping calculator task in Kotlin? My attempt is below.
fun shippingCost(amount: Double, international: Boolean): Double { if (total<=75) { total*10/100 } while (international==true){ total*15/100{ break(total>=50) } } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
19 ответов
+ 6
Here is the full runnable code from my solution -> https://code.sololearn.com/cdyDo0Yljw0m/?ref=app
+ 10
fun shippingCost(amount: Double, international: Boolean): Double {
var cost = 0.0
if (international) {
cost = if (amount * 0.15 > 50) 50.0 else amount * 0.15
}
else if (amount < 75) {
cost = amount * 0.1
}
return cost
}
fun main(args: Array<String>) {
val total = readLine()!!.toDouble()
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))
}
+ 5
You don't have a variable "total" in your function. The fuction gets it as parameter from the function call in the mainmethod. So you should rename it to "amount" to work with the user input value. Declare a new variable e.g. "costs" which takes the result and don't forget to return your that variable at the end.
+ 4
fun shippingCost(amount: Double, international: Boolean): Double {
if(international==false){
if(amount >=75.0) {
return 0.0
} else {
return amount*0.1
}
} else {
return if(amount*0.15 >50.0){
return 50.0
} else {
amount *0.15
}
}
}
fun main(args: Array<String>) {
val total = readLine()!!.toDouble()
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))
}
+ 3
fun shippingCost(amount: Double, international: Boolean): Double {
var total: Double = 0.0
if(international == true){
if(amount * 0.15 < 50){
total = amount * 0.15
}else{
total = 50.0
}
}else{
if(amount > 75){
total
}else{
total = amount * 0.1
}
}
return total
}
fun main(args: Array<String>) {
val total = readLine()!!.toDouble()
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))
}
+ 2
Full shorter and clean code
https://code.sololearn.com/csTbNS5W2o9t/?ref=app
+ 1
please resend your full code i don't do it this project
+ 1
Thank you so much Trinity ⚜ 😊
+ 1
Paramjit Singh Rana, I've already done the task with the help of Trinity ⚜:)
+ 1
SIMPLEST SOLUTION:
if(international==false){ //USA
if(amount<75) return amount*0.1
return 0.0
} else {
if(amount*0.15<50) return amount*0.15
return 50.0
}
+ 1
//This works perfectly
fun shippingCost(amount: Double, international: Boolean): Double {
var cost=0.0
if(international){
//case internationl
cost= if(amount*0.15>50)50.0 else amount*0.15
}else{
//Case Us
cost= if (amount >=75)0.0 else amount*0.10
}
return cost
}
fun main(args: Array<String>) {
val total = readLine()!!.toDouble()
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))
}
0
Muhammadaziz Ravshanbekov, even I don't know how to do none are helping..
0
Thank you so much for your time, Namrata Dattani, but I've done the task.
0
Paramjit Singh Rana the code is running fine. Put a double value in the first line of the input and a boolean value in second line than hit run.
Thank you Shobhit Pandey...you're welcome. I'm happy I could help out. 🙂
0
fun shippingCost(amount: Double, international: Boolean): Double {
var total : Double = 0.0
if (international == false){
if(amount >= 75.0){
total = 0.0
}else {
total = amount * 0.1
}
} else {
if(amount * 0.15 < 50){
total = amount * 0.15
} else {
total = 50.0
}
}
return total
}
fun main(args: Array<String>) {
val total = readLine()!!.toDouble()
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))
}
0
fun shippingCost(amount: Double, international: Boolean): Double {
if(international==false){
if(amount >=75.0) {
return 0.0
} else {
return amount*0.1
}
} else {
return if(amount*0.15 >50.0){
return 50.0
} else {
amount *0.15
}
}
}
fun main(args: Array<String>) {
val total = readLine()!!.toDouble()
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))
}
0
short and good!
fun shippingCost(amount: Double, international: Boolean): Double {
if(international==false){
if(amount >=75.0){
return 0.0} else{
return amount*0.1
}} else {return if(amount*0.15 >50.0){
return 50.0} else{amount *0.15
}}}fun main(args: Array<String>) {
val total = readLine()!!.toDouble()
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))}
0
fun main(args: Array<String>) {
println("Enter amount")
val total = readLine()!!.toDouble()
println("true or false")
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))
}
fun shippingCost(amount: Double, international: Boolean): Any {
val r = when {
international == false && amount >= 75 -> return 0.0
international == false && amount < 75 -> return amount * 0.10
(international == true) && (amount * 0.15 > 50) -> return 50.0
international == true -> return amount*0.15
else -> println("Error")
}
return r
}
0
fun shippingCost(amount: Double, international: Boolean): Double {
var cost = 0.0
if (international) {
cost = if (amount * 0.15 > 50) 50.0 else amount * 0.15
}
else if (!international) {
cost = if (amount > 75) 0.0
else amount * 0.1
}
return cost
}
fun main(args: Array<String>) {
val total = readLine()!!.toDouble()
val international = readLine()!!.toBoolean()
println(shippingCost(total, international))
}