Java入门:基础算法之计算三角形面积
本部分介绍如何计算三角形面积。
/** * @author: 理工云课堂 * @description: 程序计算三角形的面积.三角形的底和高由用户输入 */ import java.util.Scanner; class AreaTriangleDemo { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the width of the Triangle:"); double base = scanner.nextDouble(); System.out.println("Enter the height of the Triangle:"); double height = scanner.nextDouble(); //面积 = (width*height)/2 double area = (base* height)/2; System.out.println("Area of Triangle is: " + area); } }
输出
Enter the width of the Triangle: 2 Enter the height of the Triangle: 2 Area of Triangle is: 2.0