Kotlin程序:求长方体的表面积和体积


在这篇文章中,我们将了解如何计算长方体的表面积和体积。长方体的表面积使用以下公式计算:

2*( length *width + width* height + height*length)

长方体的体积使用以下公式计算:

length*width*height

以下是演示:

假设我们的输入是:

length= 6;
width= 7;
height= 8;

期望的输出是:

Volume Of the Cuboid is : 336.0
Surface area Of the Cuboid is : 292.0

算法

  • 步骤1 - 开始

  • 步骤2 - 声明五个双精度值,分别命名为length(长)、width(宽)、height(高)、volume(体积)、surfaceArea(表面积)

  • 步骤3 - 定义这些值

  • 步骤4 - 使用公式 2*(length * width + width * height + height * length) 计算长方体的表面积

  • 步骤5 - 使用公式 length * width * height 计算长方体的体积

  • 步骤6 - 显示结果

  • 步骤7 - 结束

示例1

在这个例子中,我们将使用长方体的长、宽、高来求长方体的体积和表面积。首先,声明并初始化变量length、width和height:

val length = 6
val width = 7
val height = 8

现在,使用上面的公式,求长方体的体积:

val volume = length * width * height

类似地,求长方体的表面积:

val surfaceArea =2*( length * width + width * height + height * length);

现在让我们来看最终的例子,求长方体的体积和表面积:

fun main() { val length = 6 val width = 7 val height = 8 println("The length, width and height of the cuboid is defined as $length, $width and $height") val volume = length * width * height val surfaceArea =2*( length * width + width * height + height * length); println("The volume of the cuboid is: $volume") println("The surface area of the cuboid is: $surfaceArea") }

输出

The length, width and height of the cuboid is defined as 6, 7 and 8
The volume of the cuboid is: 336
The surface area of the cuboid is: 292

示例2

在这个例子中,我们将求长方体的表面积和体积

fun main() { val length = 6 val width = 7 val height = 8 println("The length, width and height of the cuboid is defined as $length, $width and $height") cuboid(length, width, height) } fun cuboid(length: Int, width : Int, height : Int) { val volume = length * width * height val surfaceArea =2*( length * width + width * height + height * length); println("The volume of the cuboid is: $volume") println("The surface area of the cuboid is: $surfaceArea") }

输出

The length, width and height of the cuboid is defined as 6, 7 and 8
The volume of the cuboid is: 336
The surface area of the cuboid is: 292

更新于:2022年10月13日

浏览量:217

开启你的职业生涯

完成课程获得认证

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