Kotlin程式:求平行四邊形的面積


本文將講解如何計算平行四邊形的面積。平行四邊形的面積計算公式為:

base * height

下面是一個演示:

假設我們的輸入是:

Base: 6
Height: 8

期望輸出:

Area of parallelogram is: 48

演算法

  • 步驟1 − 開始。

  • 步驟2 − 宣告三個變數:底邊(base)、高(height)和結果(myResult)。

  • 步驟3 − 定義變數的值。

  • 步驟4 − 使用公式底邊 * 高計算面積,並將結果儲存。

  • 步驟5 − 顯示結果。

  • 步驟6 − 結束。

示例1

在這個例子中,我們將計算一個已知底邊和高的平行四邊形的面積。首先,我們將宣告並初始化底邊和高的變數。

val base = 5
val height = 8

然後,我們將使用上面給出的公式計算面積。平行四邊形的面積將儲存在變數myResult中。

val myResult = base * height

讓我們來看最終的例子,計算一個已知底邊和高的平行四邊形的面積。

fun main() { val base = 5 val height = 8 println("The sides of the parallelogram are defined as $base, $height, $base, $height") val myResult = base * height println("The area of parallelogram is: $myResult") }

輸出

The sides of the parallelogram are defined as 5, 8, 5, 8
The area of parallelogram is: 40

示例2

在這個例子中,我們將計算平行四邊形的面積:

fun main() { val base = 5 val height = 8 println("The sides of the parallelogram are defined as $base, $height, $base, $height") areaParallelogram(base, height) } fun areaParallelogram(base: Int, height: Int) { val myResult = base * height println("The area of parallelogram is: $myResult") }

輸出

The sides of the parallelogram are defined as 5, 8, 5, 8
The area of parallelogram is: 40

更新於:2022年10月17日

226 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.