Reputation: 159
i'm testing sides of a triangle to see if the triangle is a equilateral or isosceles, but even when all the values are equal I still get the else output. the code in question is at the bottom, i just included the rest for reference
public static void main(String[] args)
{
System.out.println(" Triangle Calculator \n");
Scanner inputab = new Scanner(System.in);
System.out.println("Input lenghts of sides 'a' and 'b':");
double sideA = inputab.nextDouble();
double sideB = inputab.nextDouble();
System.out.println("Input the size of Angle C in degrees:");
double angleC = inputab.nextDouble();
inputab.close();
double sideC = Math.sqrt((Math.pow(sideA, 2) +
Math.pow(sideB, 2)- 2*(sideA*sideB)*Math.cos(Math.toRadians(angleC))));
System.out.println("\n\t /\\\n\t / \\\n\t / \\\n\t/ \\");
System.out.printf(" %3.1f",sideA);
System.out.print("/ \\");
System.out.printf("%3.1f",sideB);
System.out.println("\n / \\\n / \\\n /______________\\");
System.out.printf("\t %3.1f\n", sideC);
double angleA = Math.asin(sideA*Math.sin(Math.toRadians(angleC))/sideC);
angleA = Math.toDegrees(angleA);
double angleB = 180 - angleC - angleA;
double perimeter = sideA + sideB + sideC;
double hf = 0.5 * (sideA + sideB + sideC);
double area = Math.sqrt(hf*(hf-sideA)*(hf-sideB)*(hf-sideC));
System.out.printf("\nSide a = ");
System.out.printf("%4.3f",sideA);
System.out.printf("\nSide b = ");
System.out.printf("%4.3f", sideB);
System.out.printf("\nSide c = ");
System.out.printf("%4.3f", sideC);
System.out.printf("\nAngle A(Deg) = ");
System.out.printf("%5.3f", angleA );
System.out.printf("\nAngle B(Deg) = ");
System.out.printf("%5.3f", angleB);
System.out.printf("\nAngle C(Deg) = ");
System.out.printf("%5.3f", angleC);
System.out.printf("\nPerimeter = ");
System.out.printf("%5.3f", perimeter);
System.out.printf("\nArea = ");
System.out.printf("%5.3f", area);
if (sideA == sideB && sideA == sideC)
{
System.out.println("\nThis is an Equilateral Triangle");
}
else
{
System.out.println("\nThis is not an Equilateral Triangle");
}
if ((sideA == sideB && sideB!= sideC )
||(sideA != sideB && sideC == sideA)
|| (sideC == sideB && sideC != sideA))
{
System.out.println("\nThis is an Isosceles Triangle");
}
else
{
System.out.println("\nThis is not an Isosceles Triangle");
}
Upvotes: 2
Views: 88
Reputation: 159
Pointy is correct. I ran eclipse in debugger mode to look at the values as the program executed. I used 5 for the values of sides A and B and 60 for angleC. The result was that A and B were equal to 5.0 while side C was equal to 4.9999... There are multiple ways you could go about fixing this, but you seem capable, especially now that you know what the issue is.
Upvotes: 2