Java中绝对值函数的介绍与其妙用中绝对值函数的介绍与其妙用
主要给大家介绍了Java中绝对值函数的介绍与其妙用,其中包括绝对值函数用来获取表达式的绝对值和绝对值
函数实现降序+升序输出。文章末尾给出了实例介绍,有需要的朋友们可以参考学习,下面来一起看看吧。
一、绝对值函数使用说明一、绝对值函数使用说明
绝对值函数是JDK中Math.java中的实现方法,其用来得到表达式的绝对值。
其实现非常简单,源码如下:
/**
* Returns the absolute value of an {@code int} value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
*
* <p>Note that if the argument is equal to the value of
* {@link Integer#MIN_VALUE}, the most negative representable
* {@code int} value, the result is that same value, which is
* negative.
*
* @param a the argument whose absolute value is to be determined
* @return the absolute value of the argument.
*/
public static int abs(int a) {
return (a < 0) ? -a : a;
}
二、绝对值的特性及其运用。二、绝对值的特性及其运用。
1、正数的绝对值是其本身。
2、负数的绝对值是其相反数。
3、零的绝对值是其本身。
绝对值:自减函数配合绝对值,先降序再升序。
int number = 6;
System.out.println("原值输出:");
while(number>=-6){
number --;
System.out.print(number+" ");
}
System.out.println("绝对值输出:");
number = 6;
while(number>=-6){
number --;
System.out.print(Math.abs(number)+" ");
}
输出结果:
原值输出:
5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7
绝对值输出:
5 4 3 2 1 0 1 2 3 4 5 6 7
三、案例三、案例
1、背景:输出如下图案。、背景:输出如下图案。
A
B A B
C B A B C
D C B A B C D
E D C B A B C D E
F E D C B A B C D E F
G F E D C B A B C D E F G
2、分析:、分析:
1、A为中心点
2、每一行,先降序,再升序