Penerangan Masalah
volum silinder = π × r² × h
of:
<code>**输入:** 半径 = 5 个单位 高度 = 10 个单位 **输出:** 体积 = 785.4 立方单位 **说明:** 使用公式计算体积: 体积 = π × 5² × 10 体积 = 785.4 立方单位</code>
<code>**输入:** 半径 = 7 个单位 高度 = 15 个单位 **输出:** 体积 = 2309.4 立方单位 **说明:** 使用公式计算体积: 体积 = π × 7² × 15 体积 = 2309.4 立方单位</code>
volume = π × r² × h
Langkah Pelaksanaanimport java.text.DecimalFormat; public class CylinderVolume { public static void main(String[] args) { double radius = 5; double height = 10; double volume = Math.PI * Math.pow(radius, 2) * height; DecimalFormat df = new DecimalFormat("0.00"); System.out.println("半径为 " + radius + ",高度为 " + height + " 的圆柱体的体积是: " + df.format(volume) + " 立方单位"); } }
<code>半径为 5.0,高度为 10.0 的圆柱体的体积是: 785.40 立方单位</code>
Kerumitan ruang:
menggunakan fungsi
Langkah Pelaksanaan
import java.text.DecimalFormat; public class CylinderVolumeFunction { static double calculateVolume(double radius, double height) { return Math.PI * Math.pow(radius, 2) * height; } public static void main(String[] args) { double radius = 5; double height = 10; double volume = calculateVolume(radius, height); DecimalFormat df = new DecimalFormat("0.00"); System.out.println("半径为 " + radius + ",高度为 " + height + " 的圆柱体的体积是: " + df.format(volume) + " 立方单位"); } }
<code>半径为 5.0,高度为 10.0 的圆柱体的体积是: 785.40 立方单位</code>
Kerumitan ruang:
Menggunakan kaedah ini, anda boleh dengan mudah mengira jumlah silinder dalam Java sambil mengekalkan kod anda mudah dan modular. Pilih kaedah yang paling sesuai dengan keperluan anda!
Atas ialah kandungan terperinci Program Java untuk mencari jumlah silinder. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!