2016-03-02 65 views
-1

我正在制定一個程序,將三角形分類爲各個類(等邊,斜角,右等),給定三角形的點。我已經完成了99%,但是有一個Else-if語句不起作用。否則 - 如果語句輸出不正確

對於最後的測試運行,我們應該輸入點(0,0)(0,5)(5,0),然後得到輸出「等邊,右」。但是,我的代碼似乎跳過了這個聲明,並直接進入下一個if語句,它將三角形分類爲scalene。

在此先感謝。我是一個新手編碼器,所以它可能是一些相對簡單的東西,它會讓我眼前一亮。

代表整個代碼。

public class TriangleCalculator { 

    @SuppressWarnings("empty-statement") 
    public static void main(String[] args) 
    { 
     System.out.println("Enter the x- and y- coordinates of the first point"); 
     Scanner keyBoard = new Scanner (System.in); 
     double coordOneX = keyBoard.nextDouble(); 
     double coordOneY = keyBoard.nextDouble(); 
     System.out.println("Enter the x- and y- coordinates of the second point"); 
     double coordTwoX = keyBoard.nextDouble(); 
     double coordTwoY = keyBoard.nextDouble(); 
     System.out.println("Enter the x- and y- coordinates of the third point"); 
     double coordThreeX = keyBoard.nextDouble(); 
     double coordThreeY = keyBoard.nextDouble(); 

     if((coordOneY - coordTwoY) * (coordTwoX - coordThreeX) == (coordTwoY - coordThreeY) * (coordOneX - coordTwoX)) 
     { 
      System.out.printf ("D1 ={(%.3f,%.3f) , D2 = (%.3f,%.3f) , D3 = (%.3f,%.3f) are colinear and cannot be vertices of a triangle.}%n", 
       coordOneX, coordOneY, coordTwoX, coordTwoY, coordThreeX, coordThreeY); 
     } 
     else if((coordOneY - coordTwoY) * (coordTwoX - coordThreeX) != (coordTwoY - coordThreeY) * (coordOneX - coordTwoX)) 
     {  
      System.out.printf ("Triangle ={(%.3f,%.3f) , (%.3f,%.3f) , (%.3f,%.3f) }%n", 
       coordOneX, coordOneY, coordTwoX, coordTwoY, coordThreeX, coordThreeY); 

      double distanceOne = Math.sqrt(Math.pow(coordOneX - coordTwoX,2) + Math.pow(coordOneY - coordTwoY,2)); 
       System.out.printf("Distance (P1,P2) = %.3f%n", distanceOne); 
      double distanceTwo = Math.sqrt(Math.pow(coordTwoX - coordThreeX,2) + Math.pow(coordTwoY - coordThreeY,2)); 
       System.out.printf("Distance (P2,P3)= %.3f%n", distanceTwo); 
      double distanceThree = Math.sqrt(Math.pow(coordOneX - coordThreeX,2) + Math.pow(coordOneY - coordThreeY,2)); 
       System.out.printf("Distance (P1,P3)= %.3f%n", distanceThree); 

      double perimeter = distanceOne + distanceTwo + distanceThree; 
       System.out.printf("Perimeter = %.3f %n", perimeter); 

      double s = perimeter/2; 
       double area = Math.sqrt(s * (s - distanceOne) * (s - distanceTwo) *(s - distanceThree)); 
       System.out.printf("Area = %.3f %n", area); 

      double side1 = distanceOne; 
      double side2 = distanceTwo; 
      double side3 = distanceThree;    
      String classification = (""); 

      if(side1 == side2) 
       classification = "isosceles"; 
      else if(side2==side3) 
        classification = "equilateral"; 
      else if (Math.abs(side1*side1 + side2*side2 - side3*side3) < 1E-9) 
       classification = "isoceles, right"; 
      if(side1 == side3||side2 == side3) 
       classification = "isosceles"; 
      else classification = "scalene"; 
      if(side1*side1 + side2*side2 == side3*side3) 
       classification = "scalene, right"; 


     System.out.println("Clasification(s): " +classification); 

這是代碼有問題的特定部分:

 if(side1 == side2) 
      classification = "isosceles"; 
     else if(side2==side3) 
       classification = "equilateral"; 
     else if (Math.abs(side1*side1 + side2*side2 - side3*side3) < 1E-9) 
      classification = "isoceles, right"; 
     if(side1 == side3||side2 == side3) 
      classification = "isosceles"; 
     else classification = "scalene"; 
     if(side1*side1 + side2*side2 == side3*side3) 
      classification = "scalene, right"; 
+0

就個人而言,我會比較的角度,以及邊,但你的代碼從來不輸出'「等邊,右」' –

回答

1

由於雙打和浮標浮點數,你不應該==比較雙打,這是因爲你真的不能100%確定或保證您分配給浮點數或雙精度的數字正是映射到內存中的數字...

在您的情況下,請改用Double.compare並看看這個article

// compares the two specified double values 
double d1 = 15.45; 
double d2 = 11.50; 
int retval = Double.compare(d1, d2); 

根據文檔:

JLS 15.21.1. Numerical Equality Operators == and !=

浮點比較的結果,如由IEEE 754標準的規格來確定,是:

Float如果一個操作數爲NaN,則==的結果是假的,但結果是=真

  • :荷蘭國際集團點相等測試是按照IEEE 754標準的規定執行。的確,當且僅當x的值是NaN時,測試x!= x纔是真的。方法Float.isNaN和Double.isNaN也可用於測試值是否爲NaN。

  • 正零和負零被認爲是相等的。例如,-0.0 == 0.0是正確的。

  • 否則,兩個不同的浮點值被相等運算符視爲不相等。特別是,有一個值代表正無窮大,一個值代表負無窮大;每個比較僅等於自身,並且每個比較與所有其他值不相等。

+0

這不會幫助。 'Double.compare()'只是將參數解包到'double'中,並且無論如何都使用'<', '>'和'=='。它增加的主要功能是支持'NaN'。 – azurefrog

相關問題