在Java中,可以使用Math.abs()
方法来计算一个数的绝对值。以下是一个简单的示例:
public class AbsoluteValue { public static void main(String[] args) { int number = -5; int absoluteValue = https://www.yisu.com/ask/Math.abs(number);"The absolute value of " + number + " is: " + absoluteValue); } }
在这个示例中,我们定义了一个名为number
的整数变量,并将其值设置为-5。然后,我们使用Math.abs()
方法计算该数的绝对值,并将结果存储在名为absoluteValue
的整数变量中。最后,我们使用System.out.println()
方法输出结果。