编写 Java 程序以查找圆锥体的体积和表面积(附带示例)。在开始编写查找圆锥体体积和表面积的 Java 程序之前,让我们先了解圆锥体表面积和体积背后的定义和公式。
Java 圆锥体表面积
如果我们知道圆锥体的半径和斜高,则可以使用以下公式计算圆锥体的表面积。
- 表面积 = 圆锥侧面积 + 圆锥底面积
- 表面积 = πrl + πr²,其中 r = 半径,l = 斜高(从圆锥顶点到底部边缘的长度)
如果我们知道圆锥体的半径和高度,则可以使用以下公式计算圆锥体的表面积。
- 表面积 = πr² +πr √h² + r²
- 我们也可以将其写为表面积 = πr (r+√h² + r²)
因为半径、高度和斜高构成了直角三角形。因此,根据勾股定理
- l² = h² + r²
- l = √h² + r²
圆锥体体积
圆锥体内部的空间量称为体积。如果我们知道圆锥体的半径和高度,则可以使用以下公式计算体积。
- 体积 = 1/3 πr²h (其中 h=圆锥的高度)
- 圆锥的侧面积 = πrl
查找圆锥体体积和表面积的 Java 程序
此 Java 程序允许用户输入圆锥体的半径和高度值。此程序将根据公式计算圆锥体的表面积、体积、边长(斜高)和侧面积。
import java.util.Scanner;
public class VolumeOfCone {
private static Scanner sc;
public static void main(String[] args) {
double radius, height;
// LSA = Lateral Surface Area of a Cone, SA = Surface Area
double length, SA, Volume, LSA;
sc = new Scanner(System.in);
System.out.println("\n Please Enter the Radius of a Cone : ");
radius = sc.nextDouble();
System.out.println("\n Please Enter the Height of a Cone : ");
height = sc.nextDouble();
length = Math.sqrt(radius * radius + height * height);
SA = Math.PI * radius * (radius + length);
Volume = (1.0/3) * Math.PI * radius * radius * height;
LSA = Math.PI * radius * length;
System.out.format("\n The Length of a Side (Slant)of a Cone = %.2f", length);
System.out.format("\n The Surface area of a Cone = %.2f", SA);
System.out.format("\n The Volume of a Cone = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cone = %.2f", LSA);
}
}

在此查找圆锥体体积和表面积的 Java 程序中,我们使用数学公式来计算圆锥体的边长(斜高)。
length = Math.sqrt(radius * radius + height * height);
接下来,我们使用数学公式计算圆锥体的表面积。
SA = Math.PI * radius * (radius + length);
在下一行,我们正在计算圆锥体的体积。
Volume = (1.0/3) * Math.PI * radius * radius * height;
在下一行 Java 代码中,我们正在计算圆锥体的侧面积。
LSA = Math.PI * radius * length;
以下 System.out.format 语句将帮助我们打印圆锥体的体积、圆锥体的侧面积、边长(斜高)和圆锥体的表面积。
System.out.format("\n The Length of a Side (Slant)of a Cone = %.2f", length);
System.out.format("\n The Surface area of a Cone = %.2f", SA);
System.out.format("\n The Volume of a Cone = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cone = %.2f", LSA);
从上面的 Java 屏幕截图中,您可以看到我们输入了圆锥体的半径 = 6,高度 = 8。
根据勾股定理,我们可以计算出母线长(侧面长度)。
l² = h² + r²
l = √h² + r² ==> √8² + 6²
= √64 + 36 ==> √100
l = 10
圆锥体的表面积是
圆锥体表面积 = πr² +πrl ==> πr (r + l)
圆锥体表面积 = Math.PI * radius * (radius + l)
圆锥体表面积 = 3.14 * 6 * (6 +10)
圆锥体表面积 = 301.44
圆锥体的体积是
圆锥体积 = 1/3 πr²h
圆锥体体积 = (1.0/3) * Math.PI * radius * radius * height
圆锥体体积 = (1.0/3) * 3.14 * 6 * 6 * 8
圆锥体体积 = 301.44
圆锥体的侧面积是
侧面积 = πrl
侧面积 = Math.PI * radius * l
侧面积 = 3.14 * 6 * 10
侧面积 = 188.4
让我们使用半径(不使用斜高)来计算圆锥体的表面积(标准公式)。
圆锥体表面积 = πr² +πr √h² + r² ==> πr (r + √h² + r²)
表面积 = Math.PI * radius * (radius + sqrt ( (height * height) + (radius * radius)))
圆锥体表面积 = 3.14 * 6 * ( 6 + √8² + 6²)
圆锥体表面积 = 3.14 * 6 * ( 6 + √100) ==> 3.14 * 6 * (16)
圆锥体表面积 = 301.44
使用函数查找圆锥体体积和表面积的 Java 程序
此 Java 程序 允许用户输入圆锥体的半径和高度值。通过使用这些值,此程序将根据公式计算圆锥体的表面积、体积、边长(斜高)和侧面积。
在此示例中,我们将使用第一个示例中指定的逻辑。但是,我们将分离逻辑并将其放在一个方法中。
package SurfaceAreaPrograms;
import java.util.Scanner;
public class VolumeOfConeUsingMethods {
private static Scanner sc;
public static void main(String[] args) {
double radius, height;
sc = new Scanner(System.in);
System.out.println(" Please Enter the Radius and Height of a Cone : ");
radius = sc.nextDouble();
height = sc.nextDouble();
VolumeOfCone(radius, height);
}
public static void VolumeOfCone(double radius, double height) {
double length, SA, Volume, LSA;
length = Math.sqrt(radius * radius + height * height);
SA = Math.PI * radius * (radius + length);
Volume = (1.0/3) * Math.PI * radius * radius * height;
LSA = Math.PI * radius * length;
System.out.format(" The Length of a Side (Slant)of a Cone = %.2f", length);
System.out.format("\n The Surface area of a Cone = %.2f", SA);
System.out.format("\n The Volume of a Cone = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cone = %.2f", LSA);
}
}
Please Enter the Radius and Height of a Cone :
5
12
The Length of a Side (Slant)of a Cone = 13.00
The Surface area of a Cone = 282.74
The Volume of a Cone = 314.16
The Lateral Surface area of a Cone = 204.20
使用面向对象编程查找圆锥体体积和表面积的 Java 程序
在此程序中,我们使用面向对象编程来划分圆锥体的体积和表面积代码。为此,我们将创建一个包含方法的类。
package SurfaceAreaPrograms;
public class VolumeOfaCone {
double length, SA, Volume, LSA;
public double LengthOfCone (double radius, double height) {
length = Math.sqrt(radius * radius + height * height);
return length;
}
public double VolumeOfCone (double radius, double height) {
Volume = (1.0/3) * Math.PI * radius * radius * height;
return Volume;
}
public double SurfaceAreaOfCone (double radius, double length) {
SA = Math.PI * radius * (radius + length);
return SA;
}
public double LateralSurfaceAreaOfCone(double radius, double length) {
LSA = Math.PI * radius * length;
return LSA;
}
}
在查找圆锥体体积和表面积的 Main Java 程序中,我们将创建上述类的实例并调用这些方法。
package SurfaceAreaPrograms;
import java.util.Scanner;
public class VolumeOfConeUsingClass {
private static Scanner sc;
public static void main(String[] args) {
double radius, height;
// LSA = Lateral Surface Area of a Cone, SA = Surface Area
double length, SA, Volume, LSA;
sc = new Scanner(System.in);
System.out.println(" Please Enter the Radius and Height of a Cone : ");
radius = sc.nextDouble();
height = sc.nextDouble();
VolumeOfaCone vc = new VolumeOfaCone();
length = vc.LengthOfCone(radius, height);
SA = vc.SurfaceAreaOfCone(radius, length);
Volume = vc.VolumeOfCone(radius, height);
LSA = vc.LateralSurfaceAreaOfCone(radius, length);
System.out.format(" The Length of a Side (Slant)of a Cone = %.2f", length);
System.out.format(" The Surface area of a Cone = %.2f", SA);
System.out.format(" The Volume of a Cone = %.2f", Volume);
System.out.format(" The Lateral Surface area of a Cone = %.2f", LSA);
}
}
Please Enter the Radius and Height of a Cone :
6
11
The Length of a Side (Slant)of a Cone = 12.53
The Surface area of a Cone = 349.28
The Volume of a Cone = 414.69
The Lateral Surface area of a Cone = 236.18
VolumeofaCone 类分析
- 首先,我们声明了一个带有两个参数的函数 LengthofCone 。在查找圆锥体体积和表面积的 Java 程序函数中,我们计算圆锥体的边长或斜高并返回值。
- 接下来,我们声明了一个带有两个参数的函数 VolumeofCone 。在该函数中,我们计算圆锥体的体积并返回值。
- 接下来,我们声明了一个带有两个参数的函数 SurfaceAreaofCone 。在该函数中,我们计算圆锥体的表面积并返回值。
- 然后我们声明了一个带有两个参数的函数 LateralSurfaceAreaofCone 。在该函数中,我们计算圆锥体的侧面积并返回值。
Main 类分析
在此查找圆锥体体积和表面积的 Java 程序中,我们首先创建了 VolumeofaCone 类的实例/对象。
VolumeOfaCone vc = new VolumeOfaCone();
接下来,我们通过传递两个参数来调用 LengthofCone 方法。这是我们用 double 数据类型创建的第一个方法,该方法将计算边长并返回值。因此,我们将返回值赋给 length 变量。
length = vc.LengthOfCone(radius, height);
接下来,我们通过传递两个参数来调用 VolumeofCone 方法。这是我们用 double 数据类型创建的第二个方法,该方法将计算圆锥体的体积并返回值。因此,我们将返回值赋给 volume 变量。
Volume = vc.VolumeOfCone(radius, height);
接下来,我们调用 SurfaceAreaofCone 方法。这是我们用 double 数据类型创建的第三个方法,该方法将计算圆锥体的表面积并返回值。因此,我们将返回值赋给 SA 变量。
SA = vcd.SurfaceAreaOfCuboid(length, width, height);
接下来,我们调用 LateralSurfaceAreaofCone 方法。这是我们用 double 数据类型创建的第四个方法,该方法将计算圆锥体的侧面积并返回值。因此,我们将返回值赋给 LSA 变量。
LSA = vc.LateralSurfaceAreaOfCone(radius, length);
最后,我们使用以下 System.out.format 语句打印边长或斜高、圆锥体的体积、圆锥体的侧面积和圆锥体的表面积。
System.out.format(" The Length of a Side (Slant)of a Cone = %.2f", length);
System.out.format("\n The Surface area of a Cone = %.2f", SA);
System.out.format("\n The Volume of a Cone = %.2f", Volume);
System.out.format("\n The Lateral Surface area of a Cone = %.2f", LSA);