JAVA——第二章2.19

JAVA——第二章2.19,第1张

问题:三角形面积 提示用户输入三角形的三个点(x1,y1),(x2,y2),(x3,y3)。然后显示它的面积
import java.util.Scanner;
public class TriangleArea {
    public static void main(String[] args) {
        System.out.println("Enter the coordinates of three points separated by spaces");
        System.out.println("like x1 y1 x2 y2 x3 y3:");
        Scanner input = new Scanner(System.in);
        double x1=input.nextDouble();
        double y1=input.nextDouble();
        double x2=input.nextDouble();
        double y2=input.nextDouble();
        double x3=input.nextDouble();
        double y3=input.nextDouble();
        double s1 = Math.pow(((x2-x1) *(x2-x1)+(y2-y1)*(y2-y1)),0.5 );
        double s2 = Math.pow(((x3-x1) *(x3-x1)+(y3-y1)*(y3-y1)),0.5 );
        double s3 = Math.pow(((x3-x2) *(x3-x2)+(y3-y2)*(y3-y2)),0.5 );
        double s = (s1+s2+s3)/2;
        double area = Math.pow((s*(s-s1)*(s-s2)*(s-s3)),0.5);
        System.out.println("The area of triangle is "+area);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/924434.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存