好吧,我很確定我的公式是正確的......不要認為我很了解如何呼叫方法,為什么我的方法不起作用?
fun main() {
println("Enter Width of the triangle")
readln()
println("Enter Height of the triangle")
ComputeMethods().hypotenuse(readln().toDouble())
}
class ComputeMethods(){
fun hypotenuse(width: Int, height: Int) {
val triangle = width.toDouble().pow(2) height.toDouble().pow(2)
val formula = "$triangle"
println(formula)
}
}
uj5u.com熱心網友回復:
您的hypotenuse函式有兩個型別的引數Int。您只給它一個,而且型別錯誤(Double)。此外,您的第一個結果readln不會存盤在任何地方。要解決它,您可以執行以下操作:
fun main() {
println("Enter Width of the triangle")
val width: Int = readln().toInt()
println("Enter Height of the triangle")
val height: Int = readln().toInt()
ComputeMethods().hypotenuse(width, height)
}
PS:要獲得斜邊,您還需要取該結果的平方根
uj5u.com熱心網友回復:
@Gilli,這是三角形公式的作業代碼示例,您必須以空格分隔格式(2.0 3.0)傳遞輸入引數,您可以按順序與給出的鏈接互動https://pl.kotl.in/mq-8jgVRy在 kotlin 操場上運行程式。
import kotlin.math.pow
fun main(args: Array<String>) {
println("Enter Width of the triangle")
val width = args[0].toDouble()
println("Enter Height of the triangle")
val height = args[1].toDouble()
ComputeMethods().hypotenuse(width, height)
}
class ComputeMethods(){
fun hypotenuse(width: Double, height: Double) {
val triangle = width.pow(2) height.pow(2)
val formula = "$triangle"
println(formula)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516808.html
標籤:科特林斜边
