找到三角形的区域和类型

时间:2014-09-10 03:53:55

标签: java types geometry area

我知道这里已经有关于三角形的Java问题了,但是我已经尝试过查找并且无法找到解决问题的方法。

我正在完成一项家庭作业,我需要提供三角形边的长度(用户可以按任何顺序提供。

我必须找到除了scalene之外的三角形的类型(右,等腰等),因为那不在说明书中。我还必须找到三角形的区域。

我有一百多行,但仍然根本不起作用。没有任何错误阻止它编译;它只是在执行时无法正常工作。

非常感谢任何帮助。

import javax.swing.JOptionPane;

public class TriangleChecker {

    public static void main(String[] args) {

        Boolean triangle, right, equilateral, isosceles;
        triangle = false;
        right = false;
        equilateral = false;
        String side1, side2, side3;
        double s1, s2, s3, perimeter, areai, bi, hi, fhi, be, he, fhe, areae, br, hr, arear;
        System.out.println("Hello welcome to the Triangle Checker");
        side1 = JOptionPane.showInputDialog("Please enter side 1 of the triangle.");
        side2 = JOptionPane.showInputDialog("Please enter side 2 of the triangle.");
        side3 = JOptionPane.showInputDialog("Please enter side 3 of the triangle.");
        s1 = Double.parseDouble(side1);
        s2 = Double.parseDouble(side2);
        s3 = Double.parseDouble(side3);
        if ((s1 > s2 + s3) || (s2 > s1 + s3) || (s3 > s1 + s2)) {
            triangle = false;
        }
        else {
            triangle = true;
        }
        if ((s1 > s2 && s1 > s3) && (s1*s1 == s2*s2 + s3*s3)) {
            right = true;
        }
        else if((s2 > s1 && s2 > s3) && (s2*s2 == s1*s1 + s3*s3)) {
            right = true;
        }
        else if((s3 > s1 && s3 > s2) && (s3*s3 == s1*s1 + s2*s2)) {
            right = true;
        }
        else {
            right = false;
        }
        if((s1 == s2) && (s1 == s3)) {
            equilateral = true;
            right = false;
            isosceles = false;
        }
        else {
            equilateral = false;

        }
        if((s1 == s2) && (s1 != s3)) {
            isosceles = true;
        }
        else if((s1 == s3) && (s1 != s2)) {
            isosceles = true;
        }
        else if((s2 == s3) && (s1 != s3)) {
            isosceles = true;
        }
        else {
            isosceles = false;
        }
        if((isosceles = true) && (s1 == s2)) {
            bi = (s3/2);
            hi = ((s1*s1) - (bi*bi));
            fhi = Math.sqrt(hi);
            areai = bi * fhi;
        }
        else if((isosceles = true) && (s1 == s3)) {
            bi = (s2/2);
            hi =((s1*s1) - (bi*bi));
            fhi = Math.sqrt(hi);
            areai = bi * fhi;
        }
        else if((isosceles = true) && (s2 == s3)) {
            bi = (s1/2);
            hi = ((s2*s2) - (bi*bi));
            fhi = Math.sqrt(hi);
            areai = bi * fhi;
        }

        else { 
            bi = 0;
            hi = 0;
            fhi = 0;
            areai = bi * fhi;
        }
        if(equilateral == true) {
            be = (s1/2);
            he = ((s2*s2) - (be*be));
            fhe = Math.sqrt(he);
            areae = be*he;
        }
        else {
            be = 0;
            he = 0;
            fhe = 0;
            areae = 0;
        }
        if((right = true) && (s1 < s2) && (s1 < s3) && (s2 < s3)) {
            br = (s1/2);
            hr =(s2);
            arear = br*hr;
        }
        else if((right = true) &&(s1 < s2) && (s1 < s3) && (s3 < s2)) {
            br = s1/1;
            hr = s3;
            arear = br*hr;
        }
        else if((right = true) && (s2 < s1) && (s2 < s3) && (s1 < s3)) {
            br = s2/2;
            hr = s1;
            arear = br*hr;
        }
        else if((right = true) && (s2 < s1) && (s2 < s3) && (s3 < s1)) {
            br = s2/2;
            hr = s3;
            arear = br*hr;
        }
        else if((right = true) && (s3 < s1) && (s3 < s2) && (s1 < s2)) {
            br = s3/2;
            hr = s1;
            arear = br*hr;
        }
        else if((right = true) && (s3 < s1) && (s3 < s2) && (s2 < s1)) {
            br = s3/2;
            hr = s2;
            arear = br*hr;
        }
        else {
            br = 0;
            hr = 0;
            arear = 0;
        }


        perimeter = s1 + s2 + s3;
        if(triangle = true) {
            System.out.println("This is a triangle.");
        }
        else {
            System.out.println("This does not equal a triangle.");
            System.exit(0);
        }
        if (right == true) {
            equilateral = false;
            isosceles = false;
        }
        else if (equilateral == true) {
            right = false;
            isosceles = false;
        }
        else {
            equilateral = false;
            right = false;
        }
        if(right = true) {
            System.out.println("This is a right triangle.");
        }
        else {
            System.out.println("This is not a right triangle.");
        }
        if (equilateral = true) {
            System.out.println("This is an equilateral triangle.");
        }
        else {
            System.out.println("This is not an equilateral triangle.");
        }
        if (isosceles = true) {
            System.out.println("This is an isosceles triangle.");
        }
        else {
            System.out.println("This is not an isosceles triangle.");
        }
        if ((arear == 0) && (areae == 0)) {
            System.out.println("The area of the triangle is " + areai + ".");
        }
        else if ((areai == 0) && (arear == 0)) {
            System.out.println("The area of the triangle is " + areae + ".");
        }
        else {
            System.out.println("The area of the triangle is " + arear + ".");
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

这种方式完全颠覆了你的问题 - 我没有真正挖掘你的代码,所以不知道为什么它失败了,但你可能会作弊一点并使用数学来找到使用的区域Heron's formula让您简化代码。

苍鹭的公式可以让你提供任何三角形(右边,等边或斜角面)三边的长度,并在不知道任何角度的情况下计算区域。

由于这听起来像是家庭作业,我不确定你的老师是否允许这种解决方案,但可能值得研究。

苍鹭的公式定义为

A = sqrt(s(s - a)(s - b)(s - c))

...其中a,b和c是边长,s定义为:

s = (a + b + c) / 2

我会把它作为练习让你弄清楚如何实现公式(如果你决定使用它)。作为提示,Math模块可以使用sqrt方法。