海诗网 知识百科 java绝对值函数(java中绝对值函数怎么用)

java绝对值函数(java中绝对值函数怎么用)

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.

*

*

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;

}

java中绝对值函数怎么用

二、绝对值的特点及其应用。

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

java中绝对值函数怎么用

情况

背景:输出以下模式。

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。然后,每行的第一个输出字母是若干行。

4.每行左右对称,每行输出的字母数=行数* 2 ^ 1(字母A);

实现:

1.执行分析中的步骤1~3。以‘a’为中心点,每一行图案按降序再升序输出。

//调用

print(5);

/**

*先降后升。

* @param row

*/

private static void print(int row){

for(int i=0;i2*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;j2*row+1;j++){

//逻辑输出字母。逻辑输出的字母,先降序再升序。

int printChar=A + Math.abs(row-j);

//如果[逻辑控制字母]大于[指定的输出字母],则:

if(printCharfirstChar){

//输出空格

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;j2*row+1;j++){

//逻辑输出字母。逻辑输出的字母,先降序再升序。

int printChar=A + Math.abs(row-j);

//如果[逻辑控制字母]大于[指定的输出字母],则:

if(printCharfirstChar){

//输出空格

System.out.print( );

}else{

//输出字母

System.out.print(((char)printChar)+ );

}

}

//输出回车

System.out.println();

}

}

java绝对值函数,以上就是本文为您收集整理的java绝对值函数最新内容,希望能帮到您!更多相关内容欢迎关注。

本文来自网络,不代表海诗网立场,转载请注明出处:https://www.hallse.com/n/169833.html
      

js(净水器多少钱一台)

发表回复
联系我们
联系我们

在线咨询: QQ交谈

邮箱: 3587015498@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们
微信扫一扫关注我们
关注微博
返回顶部