Java 程序:计算长方体表面积和体积
在本教程中,我们将学习如何在 java 中找到长方体的表面积和体积。长方体是由六个面、八个顶点和十二条边组成的三维图形。长方体的表面积等于它的六个矩形面的面积之和。长方体的体积等于一个面的面积和高度的乘积。但是在继续之前,如果您不熟悉 java 中数据类型的概念,那么一定要查看主题为Java 中数据类型的文章
输入:输入长方体的长度:5
输入长方体的宽度:7
输入长方体的高度:2
输出:长方体的表面积为:118
长方体的体积是:70
程序 1:求长方体的表面积和体积
在这个例子中,我们将看到如何在 java 中计算长方体的表面积和体积。
算法:
- 开始
- 创建扫描仪类的实例
- 声明变量来存储长方体的长度、宽度和高度。
- 要求用户初始化这些变量。
- 声明变量来存储长方体的表面积和体积。
- 使用公式计算值。
- 显示表面积和体积。
- 停下来。
下面的例子演示了如何计算长方体的表面积和体积。
//Java Program to Find the Surface Area and Volume of Cuboid
import java.util.*;
public class Main
{
// Driver Code
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of the cuboid: ");
int length=sc.nextInt();
System.out.println("Enter the breadth of the cuboid: ");
int breadth=sc.nextInt();
System.out.println("Enter the height of the cuboid: ");
int height=sc.nextInt();
int surface_area=2*((length*breadth)+(breadth*height)+(height*length));
System.out.println("The surface area of the cuboid is: "+surface_area);
int volume=length*breadth*height;
System.out.println("The volume of the cuboid is: "+volume);
}
}
输入长方体长度:4 输入长方体宽度:9 输入长方体高度:6 长方体表面积:228 长方体体积:216
程序二:求长方体的表面积和体积
在这个例子中,我们将看到如何在 java 中计算长方体的表面积和体积。
算法:
- 开始
- 创建扫描仪类的实例
- 声明变量来存储长方体的长度、宽度和高度。
- 要求用户初始化这些变量。
- 调用两个用户定义的方法来计算长方体的表面积和体积。
- 将输入的值作为参数传递。
- 声明变量来存储长方体的表面积和体积。
- 使用公式计算值。
- 返回计算值。
- 显示长方体的计算表面积和体积。
- 停下来。
下面的例子演示了如何计算长方体的表面积和体积。
//Java Program to Find the Surface Area and Volume of Cuboid
import java.util.*;
public class Main
{
// Driver Code
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of the cuboid: ");
int length=sc.nextInt();
System.out.println("Enter the breadth of the cuboid: ");
int breadth=sc.nextInt();
System.out.println("Enter the height of the cuboid: ");
int height=sc.nextInt();
//Calculate the surface area
int area= findSurfaceArea(length,breadth,height);
System.out.println("The surface area of the cuboid is: "+area);
//Calculate the volume
int vol=findVolume(length,breadth,height);
System.out.println("The volume of the cuboid is: "+vol);
}
//User-defined methood for surface area of cuboid
public static int findSurfaceArea(int length,int breadth, int height)
{
int surface_area=2*((length*breadth)+(breadth*height)+(height*length));
return surface_area;
}
//User-defined methood for volume of cuboid
public static int findVolume(int length,int breadth, int height)
{
int volume=length*breadth*height;
return volume;
}
}
输入长方体长度:9 输入长方体宽度:3 输入长方体高度:7 长方体表面积:222 长方体体积:189