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.
*
*
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("n平方根輸出:");
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
实例
环境:輸出如下所示图案设计。
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
剖析:
1、A为定位点
2、每一行,先降序,再升序
3、英文字母可以转换成整数金额,'A' = 65。那麼,每排第一个輸出英文字母为 'A' 个数。
4、每排上下对称性,每排輸出英文字母数 = 个数*2 1(英文字母A);
完成:
1、实现剖析中的1~3步。以‘A’为定位点,先降序,再升序輸出每排图案设计。
//读取
print(5);
/**
* 先降序,再升序 完成
* @param row
*/
private static void print(int row){
for(int i=0;i<2*row 1;i ){
int printChar = 'A' Math.abs(row-i);
System.out.print(((char)printChar) " ");
}
}
輸出如下所示:
F E D C B A B C D E F
2、流程4中,每排輸出英文字母数 = 个数*2 1(英文字母A),那麼:
每排应当表明的英文字母以外的一部分,打印出空格符。逻辑性操纵如下所示:
for(int j=0;j<2*row 1;j ){
//逻辑性輸出英文字母。先降序、再升序逻辑性輸出的英文字母
int printChar = 'A' Math.abs(row-j);
//假如 [逻辑性操纵英文字母] 超过 [要求輸出英文字母],则:
if(printChar>firstChar){
//輸出空格符
System.out.print(" ");
}else{
//輸出英文字母
System.out.print(((char)printChar) " ");
}
}
3、详细编码:
//详细读取
printWithRow(7);
/**
* 先倒序 再正序 輸出 英文大写字母
*
* @param row 行
*/
private static void printWithRow(int row){
for(int i=0;i
//要求輸出英文字母。每排第一个展现出的英文字母
int firstChar = 'A' i;
for(int j=0;j<2*row 1;j ){
//逻辑性輸出英文字母。先降序、再升序逻辑性輸出的英文字母
int printChar = 'A' Math.abs(row-j);
//假如 [逻辑性操纵英文字母] 超过 [要求輸出英文字母],则:
if(printChar>firstChar){
//輸出空格符
System.out.print(" ");
}else{
//輸出英文字母
System.out.print(((char)printChar) " ");
}
}
//輸出回车键
System.out.println();
}
}
以上便是小编今日的共享了,期待可以作用到大伙儿。
- END -