Reputation: 189
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.
Upvotes: 0
Views: 87
Reputation: 1232
If you are specifically wanting the answer rounded to 2 decimal places, then you also need to round the calculated area to two places. One method is as follows
Scanner sc = new Scanner(System.in);
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)) + 0.005;
long round = (long)(area * 100.0);
area = (double)round / 100.0;
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.");
First we add 0.005 so that we always get the “nearest” number when rounding down. Then multiply by 100 to move the decimal two digits to the right. Then by casting to a long you discard everything after the decimal to perform the rounding operation. Finally, cast it back to double and divide by 100 to move the decimal to the left again.
Output:
2 students got the correct answer.
Upvotes: 1
Reputation: 915
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.
public class TriangleAreaCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
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;
double delta = 1e-6; // Adjust this value based on the desired precision
System.out.println("\nEnter the answers: ");
for (int i = 0; i < 5; i++) {
ans[i] = sc.nextDouble();
if (Math.abs(ans[i] - area) < delta) {
count++;
}
}
System.out.println(count + " students got the correct answer.");
}
}
Upvotes: 2