多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Java 程序:计算三角面积 > 原文: [https://beginnersbook.com/2014/01/java-program-to-calculate-area-of-triangle/](https://beginnersbook.com/2014/01/java-program-to-calculate-area-of-triangle/) 在这里,我们将看到如何计算三角形的面积。我们将看到以下两个程序: 1)程序 1:提示用户输入三角形的基本宽度和高度。 2)程序 2:无用户交互:程序本身指定了宽度和高度。 **计划 1:** ```java /** * @author: BeginnersBook.com * @description: Program to Calculate area of Triangle in Java * with user interaction. Program will prompt user to enter the * base width and height of the triangle. */ 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(); //Area = (width*height)/2 double area = (base* height)/2; System.out.println("Area of Triangle is: " + area); } } ``` 输出: ```java Enter the width of the Triangle: 2 Enter the height of the Triangle: 2 Area of Triangle is: 2.0 ``` **计划 2:** ```java /** * @author: BeginnersBook.com * @description: Program to Calculate area of Triangle * with no user interaction. */ class AreaTriangleDemo2 { public static void main(String args[]) { double base = 20.0; double height = 110.5; double area = (base* height)/2; System.out.println("Area of Triangle is: " + area); } } ``` 输出: ```java Area of Triangle is: 1105.0 ```