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("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
解决问题。您也可以更改==
为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);
}
推荐阅读
- r - 将列添加到R中列表中的多个数据框
- python - Python - 随机化给定键的值,如果有任何重复的键,则再次随机化
- python - 如何在python中使用正则表达式提取第一段
- html - 怎么上色
- 计数器除了它们的内容吗?
- python - 如何使用 tkinter 在透明窗口上的不同位置创建 3 个(红色)不透明矩形轮廓?
- android - 有没有办法仅在使用 Android API 级别 28 上的 KeyStore 的软件中存储加密密钥?
- javascript - JavaScript 中的面向对象编程:如何从其他文件调用函数?
- python - 理解下面这段代码
- angular - 为什么我无法使用具有管理员访问权限和正确策略集的 AWS Amplify storage.get 访问我的 s3 私有对象。收到 403 错误
- python - Python 十六进制字符串到带反斜杠的十六进制字符串