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.