Kotlin 程式查詢正方形面積


在本文中,我們將瞭解如何查詢正方形的面積。正方形的面積使用以下公式計算。

side*side

以下是相同的演示 −

假設我們的輸入是 −

Length of the side : 4

所需的輸出 −

Area of the square : 16

演算法

  • 步驟 1 − 開始。

  • 步驟 2 − 宣告 2 個整數值,即 sideValue 和 myResult。

  • 步驟 3 − 定義值。

  • 步驟 4 − 使用公式 side*side 計算正方形的面積,並存儲結果。

  • 步驟 5 − 顯示結果。

  • 步驟 6 − 停止。

示例 1

在此示例中,我們將使用其邊長(如上述公式所示)查詢正方形的面積。首先,宣告並初始化一個邊長變數。

val sideValue = 4

然後,查詢正方形的面積 −

val myResult = sideValue * sideValue

讓我們現在看一個完整的示例以查詢正方形的面積 −

fun main() { val sideValue = 4 println("The length of side of the square is defined as $sideValue") val myResult = sideValue * sideValue println("The area of square is: $myResult") }

輸出

The length of side of the square is defined as 4
The area of square is: 16

示例 2

在此示例中,我們將查詢正方形的面積。

fun main() { val sideValue = 4 println("The length of side of the square is defined as $sideValue") squareArea(sideValue) } fun squareArea(sideValue: Int) { val myResult = sideValue * sideValue println("The area of square is: $myResult") }

輸出

The length of side of the square is defined as 4
The area of square is: 16

更新於: 17-Oct-2022

606 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.