Kotlin程式查詢圓的面積


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

(pie*radius*radius)/7

下面是相同的演示

假設我們的輸入是

Radius of the circle: 5

期望的輸出將是:

The Area of Circle is: 78.57142857142857

演算法

  • 步驟 1 − 開始

  • 步驟 2 − 宣告兩個變數radius和myResult

  • 步驟 3 − 從使用者處讀取radius的值

  • 步驟 4 − 使用公式(22*radius*radius)/7計算圓的面積,並將結果儲存為area

  • 步驟 5 − 顯示area的值

  • 步驟 6 − 停止

示例 1

在此示例中,我們將使用上述公式查詢圓的面積。首先,宣告並設定radius

val radius = 5

現在,計算圓的面積並將結果儲存在一個新的變數myResult中

val myResult = (22 * (radius.toDouble().pow(2)))/7

現在讓我們看看獲取圓的面積的示例:

import java.util.* import kotlin.math.pow fun main() { val radius = 5 println("The radius of the circle is defined as $radius") val myResult = (22 * (radius.toDouble().pow(2)))/7 println("Area of circle is: $myResult") }

輸出

The radius of the circle is defined as 5
Area of circle is: 78.57142857142857

示例 2

在此示例中,我們將查詢圓的面積:

import java.util.* import kotlin.math.pow fun main() { val radius = 5 println("The radius of the circle is defined as $radius") getArea(radius) } fun getArea(radius: Int) { val myResult = (22 * (radius.toDouble().pow(2)))/7 println("Area of circle is: $myResult") }

輸出

The radius of the circle is defined as 5
Area of circle is: 78.57142857142857

更新於: 2022年10月13日

1K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.