如何在 Java 中計算稜柱的體積?


稜柱指的是一個三維立體物體,它有兩個相同的端面。稜柱有兩個面,第一個是頂面和底面,第二個是側面。頂面和底面都稱為底面,它們彼此相同。所有側面也彼此相同,屬於平行四邊形類別。

當稜柱的底面是三角形時,該稜柱稱為三稜柱。類似地,當稜柱的底面是矩形時,它被稱為矩形稜柱。還有其他型別的稜柱,例如五稜柱、正方稜柱和六稜柱等。

稜柱的體積指的是稜柱佔據的空間/區域。我們可以使用稜柱的長度、寬度和高度來計算稜柱的體積。

計算三稜柱體積的公式 -

Volume = 1/2 x length x width x height

計算矩形稜柱體積的公式 -

Volume = length x width x height

在本文中,我們將瞭解如何在 Java 中找到三稜柱和矩形稜柱的體積。

向您展示一些例項

例項 1

假設我們有以下三稜柱的詳細資訊。

Length(l) is 12, width(b) is 10 and height(h) is 8

然後使用三稜柱的體積公式

Volume = 480.0

因此,矩形稜柱的體積為 480.0。

例項 2

假設我們有以下矩形稜柱的詳細資訊。

Length(l) is 4.5, width(b) is 7.5 and height(h) is 9.5 

然後使用矩形稜柱的體積公式

Volume = 320.625

因此,矩形稜柱的體積為 320.625。

例項 3

假設我們有以下三稜柱的詳細資訊

Length(l) is 8, width(b) is 6 and height(h) is 7

然後使用三稜柱的體積公式

Volume = 168.0

因此,矩形稜柱的體積為 168.0。

演算法

  • 步驟 1 - 透過初始化或使用者輸入獲取稜柱的長度、寬度和高度。

  • 步驟 2 - 使用體積公式計算稜柱的體積。

  • 步驟 3 - 列印結果。

多種方法

我們提供了不同方法的解決方案

  • 查詢矩形稜柱的體積。

  • 查詢三稜柱的體積。

讓我們逐一檢視程式及其輸出。

方法 1:查詢矩形稜柱的面積

在這種方法中,稜柱的長度、寬度和高度值將在程式中初始化。然後使用矩形稜柱的體積公式,找到體積

示例

import java.util.*; public class Main { //main method public static void main(String args[]) { //initialized length of prism double l=3.5; System.out.println("The given length of prism: "+l); //initialized width of prism double b=9; System.out.println("The given width of prism: "+b); //initialized height of prism double h=12; System.out.println("The given height of prism: "+h); //Find volume by using formula double volumeOfPrism= l * b * h; //Print the result System.out.println("Volume of rectangular prism: " +volumeOfPrism); } }

輸出

The given length of prism: 3.5
The given width of prism: 9.0
The given height of prism: 12.0
Volume of rectangular prism: 378.0

方法 2:查詢三稜柱的面積

在這種方法中,稜柱的長度、寬度和高度值將在程式中初始化。然後使用三稜柱的體積公式,找到體積。

示例

public class Main { //main method public static void main(String args[]){ //initialized length of prism double l=16; System.out.println("The given length of prism: "+l); //initialized width of prism double b=10; System.out.println("The given width of prism: "+b); //initialized height of prism double h=8; System.out.println("The given height of prism: "+h); //Find volume by using formula double volumeOfPrism= (l * b * h) / 2; //Print the result System.out.println("Volume of triangular prism: " +volumeOfPrism); } }

輸出

The given length of prism: 16.0
The given width of prism: 10.0
The given height of prism: 8.0
Volume of triangular prism: 640.0

在本文中,我們探討了如何使用不同的方法在 Java 中找到稜柱的體積。

更新於: 2022 年 10 月 27 日

788 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.