I’m learning java and have a bit of code I am trying to write that should round 24.9999999 to 25. Instead, it goes to 8.
import java.io.*;
import java.util.*;
public class RadiusOfCircle
{
public static void main(String args[])
{
Scanner kbInput = new Scanner(System.in);
System.out.print("What is the area? _");
double area = kbInput.nextInt();
System.out.println("Radius of your circle is " + Math.sqrt( area / Math.PI));
double radius = Math.sqrt( area / Math.PI);
System.out.println("\nChecking your work now...\n area = pi*(r^2)\n " + area + " = 3.14 * (" + radius + ")^2");
double radiusSqrd = Math.pow(radius, 2);
System.out.println(" " + area + " = 3.14 * " + radiusSqrd);
System.out.println(" " + area + " = " + Math.PI * radiusSqrd);
System.out.println(area + " = " + (Math.round(radiusSqrd)));
System.out.println("Are the two values the same? \nIf yes, your code is correct! \nIf no, try again!");
}
}
Also, when it asks for keyboard input of what the area is, I put in 25.
This is the output:
What is the area? _25 Radius of your circle is 2.8209479177387813 Checking your work now... area = pi*(r^2) 25.0 = 3.14 * (2.8209479177387813)^2 25.0 = 3.14 * 7.957747154594766 25.0 = 24.999999999999996 25.0 = 8 Are the two values the same? If yes, your code is correct! If no, try again!
Shouldn’t that be: