怎么利用Java实现输入三角形三点坐标得到三角形的面积和周长
时间: 2023-12-08 16:05:35 浏览: 75
您可以用以下代码来计算三角形的面积和周长:
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter x1, y1, x2, y2, x3, y3: ");
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 side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
double side2 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
double side3 = Math.pow(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2), 0.5);
double s = (side1 + side2 + side3) / 2;
double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
double perimeter = side1 + side2 + side3;
System.out.println("The area of the triangle is " + area);
System.out.println("The perimeter of the triangle is " + perimeter);
}
}
输入三角形的三个点坐标,程序即可计算出三角形的面积和周长。
阅读全文