我在 Kotlin 代码中使用数学函数“min”来声明变量“toRemove”
val toRemove = min(preferredQuantity - taken, stock.quantity)
Run Code Online (Sandbox Code Playgroud)
错误消息:Kotlin 未解析的参考
1/我可以知道如何解决吗?
2/ 该函数在kotlin.math中,为什么我不能直接使用它? https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/
谢谢!
代码目的:Class Pair 可以打印出产品名称和数量,产品名称存储在Class Product
class Pair<T, U>(var product: Product, var quantity: Int) {
for ( (product,quantity) in productAndQuantityList) {
println("Name: ${product.productName}")
println("Quantity: $quantity")
}
}
Run Code Online (Sandbox Code Playgroud)
以上错误:(2, 9) Kotlin:期望成员声明错误:(2, 57) Kotlin:函数声明必须有一个名称
class ShoppingCart{
private val productAndQuantityList = mutableListOf<Pair<Product,Int> >()
...
}
open class Product(
val productName: String,
var basePrice: Double,
open val salesPrice: Double,
val description: String) {
...}
Run Code Online (Sandbox Code Playgroud)
谢谢!
kotlin ×2