在java中使用abs()方法。我的编译器不知道该方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4412020/
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-14 17:11:00  来源:igfitidea点击:

Using abs () method in java. My compiler doesn't know the method

java

提问by Ordo

I have a simple question, but i can't find a solution for it.

我有一个简单的问题,但我找不到解决方案。

I want to use the abs ()method but it doesn't work. I'm always getting the Error:" Cannot find symbol: method abs(int)".

我想使用the abs ()方法,但它不起作用。我总是得到Error:" Cannot find symbol: method abs(int)".

I have already tried to use the method by including "import java.math"above the code. But that doens't work too.

我已经尝试通过包含"import java.math"上面的代码来使用该方法。但这也行不通。

采纳答案by Brian

Are you using:

你正在用吗:

Math.abs()

or just

要不就

abs()

?

?

Use Math.abs()

Math.abs()

回答by Marcus Leon

You have to refer to the Mathclass when you use it:

Math使用时必须引用类:

Math.abs(<intval>)

回答by Adam

It's a static method. It has to be used like this:

这是一个静态方法。它必须像这样使用:

Math.abs(int);

javadoc

文档

See Class Methods in the Java Tutorial.

请参阅 Java 教程中的类方法。

回答by Lou Franco

All functions in Java are part of a class. abs() is a static member of the Math class, so call

Java 中的所有函数都是类的一部分。abs() 是 Math 类的静态成员,因此调用

 Math.abs(val);

It's in java.lang, so no need to import anything

它在 java.lang 中,所以不需要导入任何东西

回答by Goran Jovic

Call it as

称之为

Math.abs(number)

Math.abs(number)

or import as:

或导入为:

import static java.lang.Math.abs;

import static java.lang.Math.abs;

回答by Amir Afghani

Or

或者

import static java.lang.Math.*;

回答by Mark Peters

First of all, it's java.lang.Math(your package was wrong and Math is capitalized) but that's not a problem since all classes in java.langare automatically imported.

首先,它是java.lang.Math(您的包错误并且 Math 大写)但这不是问题,因为其中的所有类java.lang都是自动导入的。

As Brian says, use Math.abs(). Or, you can import the methods statically:

正如布莱恩所说,使用Math.abs(). 或者,您可以静态导入方法:

import static java.lang.Math.*;

This will allow you to use just abs()(and all other static methods from the Mathclass) without prefixing them with Math.

这将允许您只使用abs()(以及类中的所有其他静态方法Math),而无需为它们添加前缀Math.