java绝对值函数
java绝对值函数是什么,怎么使用呢?不知道的小伙伴来看看小编今天的分享吧!
1、绝对值函数使用说明
绝对值函数是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;
}
2、绝对值的特性及其运用。
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();
}
}
-
英菲尼迪Q50L自动启停怎么用,Q50L自动启停开关位置
英菲尼迪Q50L的自动启停功能在驾驶员左侧控制面板中,采用机械按键。需要关闭时,直接按下按键位置即可。自动启停与自动驻车的使用事项如今的很多车型的配置中同时含有自动启停与自动驻...
问答查看全文>> -
一个顶俩是什么意思 一个顶俩是什么梗
一个顶俩的来源和意思 来源表情包由于近日QQ新出了成语接龙红包,一些小气的老哥不能不发,又不想别人抢,就找到了一些偏门的没法接下去的成语。一个顶俩是其中的代表,同类还有口谐辞给...
热词查看全文>> -
感谢老师最朴实的句子 小学生
1、我们理想的花朵;您用心灵清泉,润育我们情操的美果。在这不寻常的节曰里,献上我们深深的祝福!2、老师,您好!不是因为您老才叫您老师,而是跨古越今人们一直都认定“姜还是老的辣”...
句子查看全文>> -
凌渡氛围灯怎么换颜色
大众凌渡使用的氛围灯不能更换颜色,默认颜色为白色,汽车在通电的状态下,打开汽车的大灯,氛围灯就会点亮,值得一提的是,只有在高配车型上才会有氛围灯,低配车型没有氛围灯。汽车上使用...
问答查看全文>> -
郑州车辆转籍有什么流程?
郑州车辆转籍流程是:1、开据交易:缴纳二手车交易税。私户1%收取,公户4%收取;2、车辆外检:将车开到检车处,车辆进行外检、拓号、拆牌和照相,领取车辆照片,贴于检查记录表上,进...
问答查看全文>>
推荐
- 1重阳节为什么又称为“老人节”438
- 2体育综合培训心得范文153
- 3不想开学的高质量文案 不想开学的文案315
- 4领动机油加多少246
- 5马自达6是哪里的车?349
- 6英国十大手表品牌有哪些 漫威唐尼戴过什么手表355
- 7一加6手机双清会清理什么182
- 8感觉和朋友越来越疏远的句子281