Java 使用 Math 类进行计算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2481443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:14:07  来源:igfitidea点击:

Use Math class to calculate

javamath

提问by LC.

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 is 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.

编写一个计算圆柱表面积的JAVA程序,C=2(πr2)+2πrh,其中r是半径,h是圆柱的高度。允许用户输入半径和高度。将输出格式化为三位小数。使用 Math 类中的常量 PI 和方法 pow()。

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);  
    }
}

回答by Uri

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 PIMathas a symbol. It might be good to put parentheses to see this:

您忘记在 PI 和 Math 之间放置星号,因此编译器将其PIMath视为一个符号。加上括号可能会很好看:

2*(Math.PI)*(Math.pow(radius, 2))

Note that Math.PIis a statically referenced constant. Math.pow, on the other hand, is a method.

请注意,这Math.PI是一个静态引用的常量。Math.pow,另一方面,是一种方法。

All identifiers in Java are case sensitive. Math.powexists, Math.POWdoes not.

Java 中的所有标识符都区分大小写。Math.pow存在,Math.POW不存在。

Also, I've noticed that you are not using the height variable at all, so your formula calculation is incorrect.

另外,我注意到您根本没有使用高度变量,因此您的公式计算不正确。

回答by Hawkcannon

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.

原文中有一个错字:Java 区分大小写,因此 Math.POW() 在编程上与 Math.pow() 不同。此外,您不能通过连接命令进行乘法;相反,您必须在它们之间放置一个星号。

That should get it to compile.

那应该让它编译。

回答by Leif Andersen

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.

你需要在 PI 和 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.

话又说回来,有些人可能不喜欢这样。但我认为直接导入几行这样的代码会更好。

回答by trashgod

You can spare yourself the need to use Math.pow()by substituting radius * radius. Even better, factor 2πrout of the formula for the area of a Cylinder. For example, A = 2πr2+ 2πrh = 2πr(r + h).

您可以Math.pow()通过替换radius * radius. 更好的是,从圆柱面积的公式中减去 2πr。例如,A = 2πr 2+ 2πrh = 2πr(r + h)

double area = 2 * Math.PI * radius * (radius + height);

回答by M. Mott

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.

最有可能的问题是代码中计算面积的部分。编程是非常文字化的,所以当 Math.PI 和 Math.POW(radius, 2) 之间没有空格时,这会导致错误。在人眼看来,它们似乎是两种不同的表达方式,但编译器将其视为一种表达方式,因为没有另外说明。只需在 Math.PI 和 Math.POW(radius, 2) 之间添加一个 * 就可以解决您的问题。