Use Math class to calculate
Write a JAVA program that calculates the surface area of a cylinder, C= 2( π r2) + 2 π r h, where r is the radius and h i开发者_StackOverflow中文版s the height of the cylinder. Allow the user to enter in a radius and a height. Format the output to three decimal places. Use the constant PI and the method pow() from the Math class.
This is what I've done so far but it can not run. Can anyone help me ?
import Java.util.Scanner;
public class Ex5 {
public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
double radius,height,area;
System.out.print("Please enter the radius of the Cylinder");
radius = input.nextDouble();
System.out.print("Please enter the height of the Cylinder");
height = input.nextDouble();
area = (4*Math.PI*radius)+2*Math.PIMath.POW(radius,2);
System.out.println("The surface of the cylinder" + area);
}
}
If this is the source code verbatim, then there are quite a few issues here.
First, The line:
2*Math.PIMath.POW
You forgot to put an asterisk between PI and Math, so the compiler sees PIMath
as a symbol.
It might be good to put parentheses to see this:
2*(Math.PI)*(Math.pow(radius, 2))
Note that Math.PI
is a statically referenced constant. Math.pow
, on the other hand, is a method.
All identifiers in Java are case sensitive. Math.pow
exists, Math.POW
does not.
Also, I've noticed that you are not using the height variable at all, so your formula calculation is incorrect.
It might just be a typo, but you seem to have either copied your code wrong, or typed it in wrong. you have the line
area = (4*Math.PI*radius)+2*Math.PIMath.POW(radius,2);
Notice the section
*Math.PIMath
you need to have a * in between PI and Math.
Also for future reference, did you know that you can add this line to the top of your code:
import static java.lang.Math.*;
Once done, you can change that line to:
area = (4*PI*radius)+2*PI*POW(radius,2);
Then again,some people may not like this. But I think it makes for nicer code to directly import a few lines like that.
As a matter of personal taste, I tend to add parens to related statements. It makes code more readable, and can help with debugging later.
I believe that the error is in the line:
area = (4*Math.PI*radius)+2*Math.PIMath.POW(radius,2);
I would change it to:
area = (2 * Math.PI * Math.pow(radius, 2)) + (2 * Math.PI * radius * height);
There is a typo in the original: Java is case-sensitive, and thus Math.POW() is different, programmically, than Math.pow(). Also, you cannot multiply by concatenating the commands; rather you must put an asterisk between them.
That should get it to compile.
You can spare yourself the need to use Math.pow()
by substituting radius * radius
. Even better, factor 2πr out of the formula for the area of a Cylinder. For example, A = 2πr2 + 2πrh = 2πr(r + h).
double area = 2 * Math.PI * radius * (radius + height);
What most likely is the problem is the part in the code where area is being calculated. Programming is very literal, so when there is no space between Math.PI and Math.POW(radius, 2) this causes an error. To the human eye they seem like two different expressions, but the compiler reads it as one since it was not told otherwise. Simply adding a * between Math.PI and Math.POW(radius, 2) should solve your problem.
精彩评论