首页 > 解决方案 > 为什么除了“等边:三个相等的边”之外什么都没有

问题描述

我制作了这段代码来看看是什么样的三角形,但它只给出了一个答案:

public static void main(String[] args) {
    String tal1 = JOptionPane.showInputDialog("Enter the first side of the triangle ");
    Double d1 = Double.parseDouble(tal1);

    String tal2 = JOptionPane.showInputDialog("Enter the second side of the triangle");
    Double d2 = Double.parseDouble(tal2);

    String tal3 = JOptionPane.showInputDialog("Enter the Third side of the triangle");
    Double d3 = Double.parseDouble(tal3);

    if (d1<0 || d2<0 || d3 < 0)
        JOptionPane.showMessageDialog(null,"trinagle not 0");

    else if (d1 >= (d2+d3) || d3 >= (d2+d1) || d2 >= (d1+d3) )
        JOptionPane.showMessageDialog(null,"Not a triangle");

    else if (d1==d2 && d1==d3 && d2==d3 )
        JOptionPane.showMessageDialog(null,"Equilateral: Three equal sides");

    else if ((d1==d2 && d2!=d3) || (d1!=d2 && d3==d1) || (d3==d2 && d3!=d1) )
        JOptionPane.showMessageDialog(null,"Isosceles: Two equal sides");

    else if(d1!=d2 && d2!=d3 && d3!=d1)
        JOptionPane.showMessageDialog(null,"Escalene: No equal sides");

    else
        JOptionPane.showMessageDialog(null,"wrong");
    System.exit(0);
}

我已经更改了一些代码,但不知道该怎么做。

标签: java

解决方案


更改Doubledouble解决问题。您也可以更改==equals比较Double对象的值。

public static void main(String[] args) {
    String tal1 = JOptionPane.showInputDialog("Enter the first side of the triangle ");
    double d1 = Double.parseDouble(tal1);

    String tal2 = JOptionPane.showInputDialog("Enter the second side of the triangle");
    double d2 = Double.parseDouble(tal2);

    String tal3 = JOptionPane.showInputDialog("Enter the Third side of the riangle");
    double d3 = Double.parseDouble(tal3);

    if (d1 < 0 || d2 < 0 || d3 < 0) {
        JOptionPane.showMessageDialog(null, "trinagle not 0");
    } else if (d1 >= (d2 + d3) || d3 >= (d2 + d1) || d2 >= (d1 + d3)) {
        JOptionPane.showMessageDialog(null, "Not a triangle");
    } else if (d1 == d2 && d1 == d3 && d2 == d3) {
        JOptionPane.showMessageDialog(null, "Equilateral: Three equal sides");
    } else if ((d1 == d2 && d2 != d3) || (d1 != d2 && d3 == d1) || (d3 == d2 && d3 != d1)) {
        JOptionPane.showMessageDialog(null, "Isosceles: Two equal sides");
    } else if (d1 != d2 && d2 != d3 && d3 != d1) {
        JOptionPane.showMessageDialog(null, "Escalene: No equal sides");
    } else {
        JOptionPane.showMessageDialog(null, "wrong");
    }
    System.exit(0);
}

推荐阅读