Kotlin程序:求梯形面积


在本文中,我们将了解如何求梯形的面积。梯形的面积使用以下公式计算。

(height/2 * (side1 + side2))

下面是演示:

假设我们的输入是:

side1 = 5
side2 = 6
height = 6

期望输出是:

Area of trapezium is: 33.0

算法

  • 步骤 1 − 开始。

  • 步骤 2 − 声明四个整型变量:side1、side2、height和myResult。

  • 步骤 3 − 定义变量值。

  • 步骤 4 − 使用公式 (height/2 * (side1 + side2)) 计算梯形的面积并存储结果。

  • 步骤 5 − 显示结果。

  • 步骤 6 − 结束。

示例 1

在这个例子中,我们将根据梯形的底和高求梯形的面积。首先声明并初始化表示底和高的变量。

val side1 = 5
val side2 = 6
val height = 6

现在,使用上述公式求梯形的面积:

val myResult = (height/2 * (side1 + side2));

让我们来看一个求梯形面积的例子:

fun main() { val side1 = 5 val side2 = 6 val height = 6 println("The length of sides of the trapezium are defined as $side1, $side2, $height") val myResult = (height/2 * (side1 + side2)); println("The area of square is: $myResult") }

输出

The length of sides of the trapezium are defined as 5, 6, 6
The area of square is: 33

示例 2

在这个例子中,我们将求梯形的面积。

fun main() { val side1 = 5 val side2 = 6 val height = 6 println("The length of sides of the trapezium are defined as $side1, $side2, $height") trapeziumArea(side1, side2, height) } fun trapeziumArea(side1: Int, side2: Int, height: Int) { val myResult = (height/2 * (side1 + side2)); println("The area of square is: $myResult") }

输出

The length of sides of the trapezium are defined as 5, 6, 6
The area of square is: 33

更新于:2022年10月17日

浏览量:181

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.