这里我们解释如何计算三角形的面积。
三角形有三条边。三角形的面积计算公式为 a=(b*h)/2。其中 b 是底边长,h 是三角形的高度。
首先,我们需要声明类 AreaTriangle,创建一个扫描器类的对象 sc,并使用 nextDouble() 方法从用户那里读取三角形的底边长和高到变量 b,h 中。计算面积为 a=(b*h)/2。使用 System.out.println() 显示面积 a。
步骤 1:使用 public 修饰符声明类 AreaTriangle。
步骤 2:打开 main() 以启动程序,Java 程序执行从 main() 开始
步骤 3:声明变量 b,h, a 为 double 类型。
步骤 4:读取三角形的底边宽度为 b。
步骤 5:读取三角形的高度为 h。
步骤 6:计算三角形的面积为 a= (b*h)/2。
步骤 7:显示面积 a。
import java.util.Scanner;
public class AreaTriangle{
public static void main(String args[]) {
double b,h,a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base width of the Triangle:");
b = sc.nextDouble();
System.out.println("Enter the height of the Triangle:");
h = sc.nextDouble();
a = (b* h)/2;
System.out.println("The Area of the Triangle is: " + a);
}
}
Enter the base width of the Triangle:10 Enter the height of the Triangle:15 The Area of the Triangle is: 75.0