I was doing a simple program for calculating the area of a scalene triangle. We basically have to first calculate the area and then input 5 answers and check how many are correct. However, whenever I check the values a problem is arising:
System.out.println("Enter the values of the three sides: ");
double a=sc.nextDouble();
double b=sc.nextDouble();
double c=sc.nextDouble();
double s=(a+b+c)/2.0;
double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("The area of the given triangle is="+area);
double ans[]=new double[5];
int count=0;
System.out.println("\nEnter the answers: ");
for(int i=0; i<5; i++)
{
ans[i]=sc.nextDouble();
if(ans[i]==area)
{
count++;
}
}
System.out.println(count+" students got the correct answer.");
However, this is the output I'm getting.

The issue you're facing is related to the comparison of floating-point numbers in Java.
==might not work as expected due to precision issues. You need to use a tolerance or delta value when comparing floating point numbers.