Scala编程创建一个Box类,在其中定义三个变量表示一个立方体的长、宽和高,长宽高可以通过控制台输入
时间: 2023-06-02 16:03:06 浏览: 39
swakka:一个Scala库,用于通过Akka-Http以类型安全的方式创建Swagger定义
class Box(var length: Double, var width: Double, var height: Double) {
def this() {
this(0,0,0)
}
def this(length: Double, width: Double) {
this(length, width, 0)
}
def this(length: Double) {
this(length, 0, 0)
}
def volume(): Double = {
length * width * height
}
}
object Box {
def main(args: Array[String]): Unit = {
val length = scala.io.StdIn.readDouble()
val width = scala.io.StdIn.readDouble()
val height = scala.io.StdIn.readDouble()
val box = new Box(length, width, height)
println(s"Length: ${box.length}")
println(s"Width: ${box.width}")
println(s"Height: ${box.height}")
println(s"Volume: ${box.volume()}")
}
}
阅读全文