布尔类型方法 - 三角形的面积

时间:2015-10-25 22:30:12

标签: java methods boolean area

如果两个较小边的总和不等于较大边,则三角形方法(在底部)应该返回字符串"Invalid"。我创建了一个布尔方法(isValid)来测试它,但我不确定如何在三角形方法中测试它。正如您所看到的,我在第三个方法中将第一个if语句留空了,因为我无法弄清楚要实现此目的的代码。感谢您的任何意见。

 public static void main(String[] args)
{
    String inputOne;
    String inputTwo;
    String inputThree;
    double smallSide;
    double medSide;
    double largeSide;
    double sidea = 0;
    double sideb = 0;
    double sidec = 0;
    double a;
    double b;
    double c;

    inputOne = JOptionPane.showInputDialog(null, "Enter first side: ");
    smallSide = Integer.parseInt(inputOne);

    inputTwo = JOptionPane.showInputDialog(null, "Enter second side: ");
    medSide = Integer.parseInt(inputTwo);

    inputThree = JOptionPane.showInputDialog(null, "Enter third side: ");
    largeSide = Integer.parseInt(inputThree);

    if (smallSide <= medSide && medSide <= largeSide)
    {
        sidea = smallSide;
        sideb = medSide;
        sidec = largeSide;
    } else if (medSide <= smallSide && smallSide <= largeSide)
    {
        sidea = medSide;
        sideb = smallSide;
        sidec = largeSide;

    } else if (smallSide <= largeSide && largeSide <= medSide)
    {
        sidea = smallSide;
        sideb = largeSide;
        sidec = medSide;
    } else if (medSide <= largeSide && largeSide <= smallSide)
    {
        sidea = medSide;
        sideb = largeSide;
        sidec = smallSide;
    } else if (largeSide <= smallSide && smallSide <= medSide)
    {
        sidea = largeSide;
        sideb = smallSide;
        sidec = medSide;
    } else if (largeSide <= medSide && medSide <= smallSide)
    {
        sidea = largeSide;
        sideb = medSide;
        sidec = smallSide;
    }
    a = sidec;
    b = sideb;
    c = sidea;
    JOptionPane.showMessageDialog(null, triangle(a, b, c));

    if (isValid(sidea, sideb, sidec))
    {
        JOptionPane.showMessageDialog(null, area(sidea, sideb, sidec));
    }
}
public static boolean isValid(double sidea, double sideb, double sidec)
{
    double sum;

    sum = sidea + sideb;

    if (sum <= sidec)
    {
        return false;
    }
    else;
    return true;


}


 public static double area(double sidea, double sideb, double sidec)
 {
    double s;
    double next;
    double answer;

    s = (sidea + sideb + sidec) / 2; 
    next = (s * sidea) * (s * sideb) * (s * sidec);
    answer = s * next;
    return Math.sqrt(answer);



 }

 public static String triangle(double a, double b, double c)
 {
    if ()
        return "Invalid Triangle";
    else if (a == c)
        return "Equilateral";
    else if (a == b || b == c)
        return "Isosceles";
    else 
        return "Scalene";
 }

3 个答案:

答案 0 :(得分:0)

不会是这样的吗?:

 if(isValid(a,b,c)==false)

 if(!isValid(a,b,c)) 

基本上是说&#34; if(无效)&#34;

答案 1 :(得分:0)

我想,你刚刚在纸上写了这个算法,然后编写代码。 例如 1.找到最大的一面 2.统计其他人的总和 3.比较结果 4.显示信息 然后你明白你要写什么

答案 2 :(得分:0)

无需应用如此多的if-else条件来安排双方。您可以省略它们,并可以将isValid方法修改为:

public static boolean isValid(double sidea, double sideb, double sidec) {
    if ((sidea + sideb > sidec) 
    && (sideb + sidec > sidea) 
    && (sidec + sidea > sideb)) {
        return true;
    }
    return false;
}

这与实现逻辑相同,即在三角形中,任意两边的总和总是严格大于第三边。

现在,在方法三角形中,第一个if语句可以写成:

if (!isValid(a,b,c)) {
    return "Invalid Triangle";
}