if语句中的JOptionPane

时间:2014-09-30 00:30:59

标签: java swing joptionpane

对于我的作业,我使用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));


     } 
     }

2 个答案:

答案 0 :(得分:3)

在比较三角形边的“if语句”之前,您肯定缺少“else”关键字。试试这个:

if (a==b) JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
else if (b==c) JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");
else if (a==c) JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");

或者更简洁:

if (a==b || b==c || c==a) JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");

另一个问题是 if (a==b) { if (b==c) { } }

不一样

if ((a==b) && (b==c)) { }

在第一种情况下,如果a == b但b!= c则不会执行任何操作,而在第二种情况下,您的代码将跳到下一个else if语句。

答案 1 :(得分:0)

另一种在不那么混乱的庄园中组织这种方法的方法是分别检查每个属性。创建单独的方法来确定您需要知道的每件事。然后所有案例都没有混在一起。类似的东西:

int angle = getTriangleAngle(a,b,c);
boolean isocelesTriangle = isIsocelesTriangle(a,b,c);
boolean equalateralTriangle = isEqualateralTriangle(a,b,c);
int area = calcTriangleArea(a,b,c);

然后你可以从这些值构建输出字符串。