1-2计算三角形的面积。输入三角形的三个点(x1,y1),(x2,y2)和(x3,y3),计算三解形的面积。
(1)程序代码如下:
package cn.edu.jxufe.denghuan;
import java.util.Scanner;
public class TriangleArea {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter three points for a triangle:");
double x1=input.nextDouble();
double y1=input.nextDouble();
double x2=input.nextDouble();
double y2=input.nextDouble();
double x3=input.nextDouble();
double y3=input.nextDouble();
//Math.pow(a, b)返回值为a的b次幂;
double a=Math.pow(x1-x2,2)+Math.pow(y1-y2,2);
double side1=Math.pow(a, 0.5);
double b=Math.pow(x1-x3,2)+Math.pow(y1-y3,2);
double side2=Math.pow(b, 0.5);
double c=Math.pow(x2-x3,2)+Math.pow(y2-y3,2);
double side3=Math.pow(c, 0.5);
double s=(side1+side2+side3)/2;
double area=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
System.out.println("The area of the triangle is "+(int)(area*10)/10.0);
}
}
三解形的面积
最新推荐文章于 2021-02-16 00:11:00 发布