我正在制定一個程序,將三角形分類爲各個類(等邊,斜角,右等),給定三角形的點。我已經完成了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";
就個人而言,我會比較的角度,以及邊,但你的代碼從來不輸出'「等邊,右」' –