编写 Java 程序,计算圆柱体的体积和表面积,并附带示例。在开始编写计算圆柱体体积和表面积的 Java 程序之前,让我们先看看圆柱体表面积、顶部或底部表面积、侧面表面积和圆柱体体积的定义和公式。
Java 圆柱体表面积
如果我们知道圆柱体的半径和高度,就可以使用以下公式计算圆柱体的表面积:
圆柱体表面积 = 2πr² + 2πrh(其中 r 是半径,h 是圆柱体的高度)。
Java 圆柱体体积
圆柱体内部的空间称为体积。如果我们知道圆柱体的高度,就可以使用以下公式计算圆柱体的体积:
- 圆柱体体积 = πr²h
- 圆柱体侧面积 = 2πrh
- 我们可以计算圆柱体顶部或底部表面积 = πr²
查找圆柱体体积和表面积的 Java 程序
这个 Java 程序 允许用户输入半径和高度的值。接下来,Java 程序将根据公式计算圆柱体的体积、圆柱体的侧面表面积、圆柱体的表面积以及圆柱体的顶部或底部表面积。
// Java Program to find Volume and Surface Area of a Cylinder
package SurfaceAreaPrograms;
import java.util.Scanner;
public class VolumeOfCylinder {
private static Scanner sc;
public static void main(String[] args) {
// L = Lateral Surface Area of a Cylinder, T = Top Surface Area
double radius, sa, Volume, height, L, T;
sc = new Scanner(System.in);
System.out.println("\n Please Enter the radius of a Cylinder : ");
radius = sc.nextDouble();
System.out.println("\n Please Enter the Height of a Cylinder : ");
height = sc.nextDouble();
sa = 2 * Math.PI * radius * (radius + height);
Volume = Math.PI * radius * radius * height;
L = 2 * Math.PI * radius * height;
T = Math.PI * radius * radius;
System.out.format("\n The Surface area of a Cylinder = %.2f", sa);
System.out.format("\n The Volume of a Cylinder = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L);
System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T);
}
}

以下语句将允许用户输入圆柱体的半径和高度。然后我们将用户输入的值赋给已声明的变量 radius 和 height。
System.out.println("\n Please Enter the radius of a Cylinder : ");
radius = sc.nextDouble();
System.out.println("\n Please Enter the Height of a Cylinder : ");
height = sc.nextDouble();
接下来,我们使用数学公式计算圆柱体的表面积。
sa = 2 * Math.PI * radius * (radius + height);
在下一行 Java 代码中,我们计算圆柱体的体积。
Volume = Math.PI * radius * radius * height;
在下一行,我们计算圆柱体的侧面表面积和圆柱体的顶部或底部表面积。
L = 2 * Math.PI * radius * height; T = Math.PI * radius * radius;
以下 System.out.format 语句将帮助我们打印出圆柱体的体积、圆柱体的侧面表面积、圆柱体的表面积以及圆柱体的顶部或底部表面积。
System.out.format("\n The Surface area of a Cylinder = %.2f", sa);
System.out.format("\n The Volume of a Cylinder = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L);
System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T);
从上面的截图可以看出,我们输入的圆柱体半径为 4,高度为 6。
圆柱体的表面积是
圆柱体表面积 = 2πr² + 2πrh
也可以写成
圆柱体表面积 = 2πr (r+h)
圆柱体表面积 = 2 * Math.PI * radius * (radius + height)
圆柱体表面积 = 2 * 3.14 * 4 * (4 + 6)
圆柱体表面积 = 251.2
圆柱体的体积是
圆柱体体积 = πr²h
圆柱体体积 = Math.PI * radius * radius * height
圆柱体体积 = 3.14 * 4 * 4 * 6
圆柱体体积 = 301.44
圆柱体的侧面积是
LSA = 2πrh
LSA = 2 * Math.PI * radius * height
LSA = 2 * 3.14 * 4 * 6
LSA = 150.72
圆柱体顶部或底部表面积是
T = πr²
T = Math.PI * radius * radius
T = 3.14 * 4 * 4
T = 50.24
注意:为了计算方便,我们取 π = 3.14 而不是 (3.142857142..)。因此,以上所有值都与程序输出值接近,但可能在 0.01 的范围内有所不同。
使用函数计算圆柱体体积和表面积的 Java 程序
这个 Java 程序 允许用户输入半径和高度的值。接下来,这个 Java 程序将根据公式计算圆柱体的体积、圆柱体的侧面表面积、圆柱体的表面积以及圆柱体的顶部或底部表面积。在这个例子中,我们使用了第一个示例中指定的逻辑,但将其放入了一个方法中。
package SurfaceAreaPrograms;
import java.util.Scanner;
public class VolumeOfCylinderUsingMethods {
private static Scanner sc;
public static void main(String[] args) {
double radius, height;
sc = new Scanner(System.in);
System.out.println("\n Please Enter the radius of a Cylinder : ");
radius = sc.nextDouble();
System.out.println("\n Please Enter the Height of a Cylinder : ");
height = sc.nextDouble();
VolumeOfCylinder(radius, height);
}
public static void VolumeOfCylinder (double radius, double height) {
// L = Lateral Surface Area of a Cylinder, T = Top Surface Area
double sa, Volume, L, T;
sa = 2 * Math.PI * radius * (radius + height);
Volume = Math.PI * radius * radius * height;
L = 2 * Math.PI * radius * height;
T = Math.PI * radius * radius;
System.out.format("\n The Surface area of a Cylinder = %.2f", sa);
System.out.format("\n The Volume of a Cylinder = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L);
System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T);
}
}
Please Enter the radius of a Cylinder :
3
Please Enter the Height of a Cylinder :
5
The Surface area of a Cylinder = 150.80
The Volume of a Cylinder = 141.37
The Lateral Surface area of a Cylinder = 94.25
The Top OR Bottom Surface Area of a cylinder = 28.27
使用面向对象编程计算圆柱体体积和表面积的 Java 程序
在这个 Java 程序中,我们将圆柱体体积和表面积的代码进行面向对象编程的划分。为此,我们将创建一个包含方法的类。
package SurfaceAreaPrograms;
public class VolumeOfACylinder {
double sa, Volume, L, T;
public double VolumeOfCylinder (double radius, double height) {
Volume = Math.PI * radius * radius * height;
return Volume;
}
public double SurfaceAreaOfCylinder (double radius, double height) {
sa = 2 * Math.PI * radius * (radius + height);
return sa;
}
public double LateralSurfaceAreaOfCylinder (double radius, double height) {
L = 2 * Math.PI * radius * height;
return L;
}
public double TotalSurfaceAreaOfCylinder (double radius) {
T = Math.PI * radius * radius;
return T;
}
}
在主要的计算圆柱体体积和表面积的 Java 程序中,我们将创建上述类的实例并调用方法。
package SurfaceAreaPrograms;
import java.util.Scanner;
public class VolumeOfCylinderUsingClass {
private static Scanner sc;
public static void main(String[] args) {
double radius, height, sa, Volume, L, T;
sc = new Scanner(System.in);
System.out.println("\n Please Enter the radius of a Cylinder : ");
radius = sc.nextDouble();
System.out.println("\n Please Enter the Height of a Cylinder : ");
height = sc.nextDouble();
VolumeOfACylinder vac = new VolumeOfACylinder();
sa = vac.SurfaceAreaOfCylinder(radius, height);
Volume = vac.VolumeOfCylinder(radius, height);
L = vac.LateralSurfaceAreaOfCylinder(radius, height);
T = vac.TotalSurfaceAreaOfCylinder(radius);
System.out.format("\n The Surface area of a Cylinder = %.2f", sa);
System.out.format("\n The Volume of a Cylinder = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L);
System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T);
}
}
Please Enter the radius of a Cylinder :
5
Please Enter the Height of a Cylinder :
9
The Surface area of a Cylinder = 439.82
The Volume of a Cylinder = 706.86
The Lateral Surface area of a Cylinder = 282.74
The Top OR Bottom Surface Area of a cylinder = 78.54
AreaOfACylinder 类分析
- 首先,我们声明了一个带有两个参数的 VolumeofCylinder 函数。在计算圆柱体体积和表面积的 Java 程序函数中,我们计算圆柱体的体积并返回值。
- 接下来,我们声明了一个带有两个参数的 SurfaceAreaofCylinder 函数。在该函数中,我们计算圆柱体的表面积并返回值。
- 接下来,我们声明了一个带有两个参数的 LateralSurfaceAreaofCylinder 函数。在该函数中,我们计算圆柱体的侧面表面积并返回值。
- 在这里,我们声明了一个带有一个参数的 TotalSurfaceAreaofCylinder 函数。在该函数中,我们计算圆柱体的总表面积并返回值。
Main 类分析
在这个计算圆柱体体积和表面积的 Java 程序中,我们创建了一个 AreaOfACylinder 类的实例/对象。
VolumeOfACylinder vac = new VolumeOfACylinder();
接下来,我们调用 VolumeofCylinder 方法。这是我们创建的第一个 double 类型的方法,该方法将计算圆柱体的体积并返回一个值。因此,我们将返回值赋给 volume 变量。
Volume = vac.VolumeOfCylinder(radius, height);
接下来,我们调用 SurfaceAreaofCylinder 方法。这是我们创建的第二个 double 类型的方法,该方法将计算圆柱体的表面积并返回一个值。因此,我们将返回值赋给 sa 变量。
sa = vac.SurfaceAreaOfCylinder(radius, height);
接下来,我们调用 LateralSurfaceAreaofCylinder 方法。这是我们创建的第三个 double 类型的方法。该方法将计算圆柱体的侧面表面积并返回一个值。因此,我们将返回值赋给 L 变量。
L = vac.LateralSurfaceAreaOfCylinder(radius, height);
接下来,我们调用 TotalSurfaceAreaofCylinder 方法。这是我们创建的第四个 double 类型的方法。该方法将计算圆柱体的总表面积并返回一个值。因此,我们将返回值赋给 T 变量。
T = vac.TotalSurfaceAreaOfCylinder(radius);
最后,我们使用以下 System.out.format 语句打印 Java 圆柱体的体积、圆柱体的侧面表面积、圆柱体的表面积以及圆柱体的顶部或底部表面积。
System.out.format("\n The Surface area of a Cylinder = %.2f", sa);
System.out.format("\n The Volume of a Cylinder = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cylinder = %.2f", L);
System.out.format("\n The Top OR Bottom Surface Area of a cylinder = %.2f", T);