Java 为什么它除了“等边:三条等边”之外没有其他意义
我编写了这个代码来查看三角形的类型,但它只给出了一个答案:Java 为什么它除了“等边:三条等边”之外没有其他意义,java,Java,我编写了这个代码来查看三角形的类型,但它只给出了一个答案: 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("E
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);
}
我已经更改了一些代码,但不知道该怎么办。将双精度更改为双精度可修复此问题。您还可以将==更改为等于,以比较Double对象的值
您的问题是您正在比较对象而没有值
因此,您可以将Double更改为Double来比较值,也可以更改运算符-例如,将d1==d2更改为sd1.equalsd2。与equals进行比较,而不是与==进行比较,或者如果要与==进行比较,可以使用Double而不是Double。双重需要equals@XtremeBaumer哎呀,误读了。格式让我明白了。谢谢你的回复。来吧,在我检查这里之前,用同样的答案
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);
}