计算给定 3 个用户定义点的三角形面积 - 初学者 [英] calculate area of triangle given 3 user defined points -Beginner

查看:17
本文介绍了计算给定 3 个用户定义点的三角形面积 - 初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以编译我的代码,但它不会生成所需的区域.我不确定我在哪里绊倒了.

I can get my code to compile, but it doesn't produce the area that is desired. I'm not sure where I have stumbled.

他们希望您让用户为三角形的 3 个点输入 6 个坐标(x 和 y 值)并获得面积.我的代码如下:

They want you to have the user enter 6 coordinates (x and y value) for the 3 points of a triangle and get the area. My code is as follows:

import java.util.Scanner;

public class AreaTriangle {
    // find the area of a triangle
    public static void main (String [] args) {
        double side1 = 0;
        double side2 = 0;
        double side3 = 0;

        Scanner input = new Scanner(System.in);

        //obtain three points for a triangle
        System.out.print("Enter three points for a triangle (x and y intercept): ");
        double side1x  = input.nextDouble();
        double side1y  = input.nextDouble();
        double side2x  = input.nextDouble();
        double side2y  = input.nextDouble();
        double side3x  = input.nextDouble();
        double side3y  = input.nextDouble();

        //find length of sides of triangle
        side1 = Math.pow(Math.pow((side2x - side1x), 2) + Math.pow((side2y - side1y), 2) * .05, side1);
        side2 = Math.pow(Math.pow((side3x - side2x), 2) + Math.pow((side3y - side2y), 2) * .05, side2);
        side3 = Math.pow(Math.pow((side1x - side3x), 2) + Math.pow((side1y - side3y), 2) * .05, side3);

        double s = (side1 + side2 + side3) / 2;

        double area = Math.sqrt(s * (s - side1) * (s - side2) * (s-side3)) * 0.5;

        System.out.println("area" + area);
    }
}

推荐答案

你应该尝试实现这个等式.http://www.mathopenref.com/coordtrianglearea.html

You should try implementing this equation. http://www.mathopenref.com/coordtrianglearea.html

这篇关于计算给定 3 个用户定义点的三角形面积 - 初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆