我正在研究一个程序,将三角形分类为三角形的各个点(等边,斜角,右边等)。我已经完成了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%确定或保证你赋予float或double的数字是完全正确的在内存中映射的内容......
在您的情况下,请使用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标准规范确定的浮点比较结果如下:
根据IEEE 754标准的规则执行浮点相等测试:
如果任一操作数为NaN,则==的结果为false,但!=的结果为true。实际上,当且仅当x的值是NaN时,测试x!= x才为真。方法Float.isNaN和Double.isNaN也可用于测试值是否为NaN。
正零和负零被认为是相等的。例如,-0.0 == 0.0为真。
否则,等于运算符会将两个不同的浮点值视为不相等。特别是,有一个值代表正无穷大,一个值代表负无穷大;每个比较仅与自身相等,每个比较不等于所有其他值。