如果语句和JOptionPane

时间:2014-09-30 23:09:20

标签: if-statement joptionpane

该程序假设用户使用JOptionPane输入3个边,然后告诉它们它是什么类型的三角形并使用JOptionPane计算区域。

我让程序工作,但后来我意识到我需要添加第一个if语句,如果a,b或c大于其他2个边的总和那么它将打印出没有三角形。

我的问题是,当第一个if语句为真时,它的工作原理。但是如果它不是真的它告诉我没有三角形并结束程序而不去其余的if语句。

 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+c || b>=a+c || c>=a+b); {
               JOptionPane.showMessageDialog(null, "There is no triangle");
               System.exit(0); }

    }if ((a==b) && (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 || b==c || c==a)
               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 || b==c || c==a)
                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 || b==c || c==a)
                JOptionPane.showMessageDialog(null, "The triangle is a Isosceles triangle");

    }

    JOptionPane.showMessageDialog(null, "The area of the triangle is: " + Math.sqrt((s)*(s - a)*(s - b)*(s - c)));

    } 
  }

1 个答案:

答案 0 :(得分:0)

你有一个错误的分号,它终止了if语句。

if (a>=b+c || b>=a+c || c>=a+b); {

应该

if (a>=b+c || b>=a+c || c>=a+b) {
               JOptionPane.showMessageDialog(null, "There is no triangle");
               System.exit(0); }