2014-09-30 123 views
0

該程序假設有用戶使用JOptionPane輸入三面,然後告訴它們是什麼類型的三角形,並使用JOptionPane計算面積。如果語句和JOptionPane

我有程序工作,但後來我意識到我需要添加第一個if語句,如果a,b或c大於其他兩邊的總和,那麼它將打印出沒有三角形的地方。

我的問題是,當第一個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))); 

    } 
    } 
+0

if語句不工作...壓痕沒有幫助! – Ian 2014-09-30 23:15:00

回答

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); }