【发布时间】:2014-09-30 00:30:58
【问题描述】:
对于我的作业,我使用 JOptionPane 要求用户输入三角形的 3 个边。然后假设该程序使用 JOptionPane 告诉他们三角形的类型(等边、右、锐角、钝角和等腰三角形,但等腰三角形也是直角、钝角或锐角)并计算面积。
当它是等边的时候,它回来告诉我它是等边的,但它也告诉我它是等腰的 3 次。即使我只有等腰 JOptionPane 和其他类型的三角形。
我的另一个问题是,当它是正确的、尖锐的或钝的时,它会跳过 JOptionPane 告诉我它是什么类型,而只是告诉我它是等腰的。
package assignment.ii;
import javax.swing.JOptionPane;
import java.lang.*;
public class AssignmentII
{
public static void main(String[] args) {
int a = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a side of the triangle "));
int b = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a side of the triangle "));
int c = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a side of the triangle "));
double s = (.5*(a+b+c));
if(a==b){
if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a equilateral triangle");
}else if (((a*a)+(b*b)) == (c*c)) {
JOptionPane.showMessageDialog(null, "The triangle is a right triangle");
if (a==b)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (a==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
}else if (((a*a)+(b*b))<(c*c)){
JOptionPane.showMessageDialog(null, "The triangle is an obtuse triangle");
if (a==b)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (a==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
}else if (((a*a)+(b*b))>(c*c))
JOptionPane.showMessageDialog(null, "The triangle is an acute triangle");
if (a==b)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (b==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
if (a==c)
JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
double d;
d = ((s)*(s - a)*(s - b)*(s - c));
JOptionPane.showMessageDialog(null, "The area of the triangle is: " + Math.sqrt(d));
}
}
【问题讨论】:
-
您最后的
else if缺少两个大括号。当a==b但b!=c什么都不会被分析。 -
仅供参考:您不需要在您的代码中需要
import java.lang.*;,因为无论如何它都会由编译器完成。 -
您是否假设第三个条目 (c) 是斜边?在根据勾股定理检查输入之前,您可能需要检查输入以确定哪个是最大的。
标签: java swing joptionpane