当前位置:家常菜>百科>科技>正文

java绝对值函数

人气:348 ℃/2023-03-24 05:05:02

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();

}

}

  • 新手开车遇事故解决办法

    1、遇到一点小的擦碰事故时,要立刻停车,在车后树立三角警示牌,这是必须需要做的,通过摆放三角警示牌对其它过往汽车做好提醒,防止有二次事故。2、互相查看证件,行驶证和汽车驾驶证,...

    问答查看全文>>
  • 小车的后视镜怎么调?

    车内后视镜的调节方法:1、在调整后视镜前,必须调整好车辆座椅及方向盘的位置;2、司机位侧门上的旋转扭可调节中央的后视镜,上面有操作介绍的;3、调整车内的后视镜,将其里面的地平线...

    问答查看全文>>
  • gl8油耗2.0t

    使用2.0t发动机的别克gl8百公里油耗是12.5升。别克gl8是别克旗下的一款mpv车型,新款别克gl8已经换装了新款的LSY发动机,这款发动机的燃油经济性是更好的,这款发动...

    问答查看全文>>
  • 郭敬明经典语录2021年

    1、自己越在乎的人自己就越不能承受他对自己不好。2、飞机场的骚乱一会儿就停止了,这里的人都是有着自己的方向的,匆匆的起飞,匆匆的下降,带走别人的故事,留下自己的回忆。习惯了你对...

    语录查看全文>>
  • 舒客儿童电动牙刷F2值得买吗 舒客儿童电动牙刷F2怎么样

    舒客儿童电动牙刷F2主打Q萌儿童专用,那么舒客儿童电动牙刷F2怎么样?舒客儿童电动牙刷F2值得买吗?面对这些问题,以下是针对舒客儿童电动牙刷F2进行的全面分析,以了解舒客儿童电...

    品牌查看全文>>
本网站部分内容、图文来自于网络,如有内容侵犯您的合法权益,请及时与我们联系,我们将第一时间安排核实及删除!
CopyRight © 2007-2024 家常菜 All Rights Reserved. 手机版