import java.util.Scanner;//此题是建立在第21题的基础上的 循序渐进
虽然我已经快学完集合类了。这些题目看上去我都会做。但看的懂与实际上敲出来是有差距的。尤其当你还是一个新手,敲代码没有达到十万行时,多敲敲代码是很有好处的。
public class AreaOfTriangle21 //计算三角形面积
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("请输入坐标 x1 y1 x2 y2 x3 y3:");
double firstPointx1=input.nextDouble();//接收x1
double firstPointy1=input.nextDouble(); //接收y1
double secondPointx2=input.nextDouble();//接收x2
double secondPointy2=input.nextDouble(); //接收y2
double thirdPointx3=input.nextDouble();//接收x3
double thirdPointy3=input.nextDouble(); //接收y3
double firstPoint=Math.pow(secondPointx2-firstPointx1,2);
double secondPoint=Math.pow(secondPointy2-firstPointy1,2);
double side1=Math.pow(firstPoint+secondPoint,0.5);//计算(x1,y1)到(x2,y2)的两点间距离
firstPoint=Math.pow(thirdPointx3-firstPointx1,2);
double thirdPoint=Math.pow(thirdPointy3-firstPointy1,2);
double side2=Math.pow(firstPoint+thirdPoint,0.5);//计算(x1,y1)到(x3,y3)的两点间距离
secondPoint=Math.pow(thirdPointx3-secondPointx2,2);
thirdPoint=Math.pow(thirdPointy3-secondPointy2,2);
double side3=Math.pow(thirdPoint+secondPoint,0.5);//计算(x3,y3)到(x2,y2)的两点间距离
double s=(side1+side2+side3)/2;
double area=Math.pow(s*(s-side1)*(s-side2)*(s-side3),0.5);//计算三角形面积
System.out.println("三角形的面积是"+(int)(area*100)/100.0);
}
}
/*
请输入坐标 x1 y1 x2 y2 x3 y3:1.5 -3.4 4.6 5 9.5 -3.4
三角形的面积是33.6
*/ |