最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

java絕對值函數(shù)

來源:懂視網(wǎng) 責(zé)編:李贏贏 時間:2022-03-23 19:43:55
文檔

java絕對值函數(shù)

一、絕對值函數(shù)使用說明:絕對值函數(shù)是JDK中Math.java中的實(shí)現(xiàn)方法,其用來得到表達(dá)式的絕對值。二、絕對值的特性及其運(yùn)用:1、正數(shù)的絕對值是其本身。2、負(fù)數(shù)的絕對值是其相反數(shù)。3、零的絕對值是其本身。
推薦度:
導(dǎo)讀一、絕對值函數(shù)使用說明:絕對值函數(shù)是JDK中Math.java中的實(shí)現(xiàn)方法,其用來得到表達(dá)式的絕對值。二、絕對值的特性及其運(yùn)用:1、正數(shù)的絕對值是其本身。2、負(fù)數(shù)的絕對值是其相反數(shù)。3、零的絕對值是其本身。

java絕對值函數(shù)是什么,怎么使用呢?不知道的小伙伴來看看小編今天的分享吧!

一、絕對值函數(shù)使用說明

絕對值函數(shù)是JDK中Math.java中的實(shí)現(xiàn)方法,其用來得到表達(dá)式的絕對值。

其實(shí)現(xiàn)非常簡單,源碼如下:

/**

* 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;

}

二、絕對值的特性及其運(yùn)用。

1、正數(shù)的絕對值是其本身。

2、負(fù)數(shù)的絕對值是其相反數(shù)。

3、零的絕對值是其本身。

絕對值:自減函數(shù)配合絕對值,先降序再升序。

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)+" ");

}

輸出結(jié)果:

原值輸出:

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為中心點(diǎn)

2、每一行,先降序,再升序

3、字母可以換算成整數(shù),'A' = 65。那么,每行首個輸出字母為 'A' +行數(shù)。

4、每行左右對稱,每行輸出字母數(shù) = 行數(shù)*2 +1(字母A);

實(shí)現(xiàn):

1、實(shí)現(xiàn)分析中的1~3步。以‘A’為中心點(diǎn),先降序,再升序輸出每行圖案。

//調(diào)用

print(5);

/**

* 先降序,再升序 實(shí)現(xiàn)

* @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中,每行輸出字母數(shù) = 行數(shù)*2 +1(字母A),那么:

每行應(yīng)該顯示的字母除外的部分,打印空格。邏輯控制如下:

for(int j=0;j<2*row+1;j++){

//邏輯輸出字母。先降序、再升序邏輯輸出的字母

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

//如果 [邏輯控制字母] 大于 [規(guī)定輸出字母],則:

if(printChar>firstChar){

//輸出空格

System.out.print(" ");

}else{

//輸出字母

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

}

}

3、完整代碼:

//完整調(diào)用

printWithRow(7);

/**

* 先倒序 再正序 輸出 英文大寫字母

*

* @param row 行

*/

private static void printWithRow(int row){

for(int i=0;i

//規(guī)定輸出字母。每行第一個顯示出來的字母

int firstChar = 'A' + i;

for(int j=0;j<2*row+1;j++){

//邏輯輸出字母。先降序、再升序邏輯輸出的字母

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

//如果 [邏輯控制字母] 大于 [規(guī)定輸出字母],則:

if(printChar>firstChar){

//輸出空格

System.out.print(" ");

}else{

//輸出字母

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

}

}

//輸出回車

System.out.println();

}

}

以上就是小編今天的分享了,希望可以幫助到大家。

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

java絕對值函數(shù)

一、絕對值函數(shù)使用說明:絕對值函數(shù)是JDK中Math.java中的實(shí)現(xiàn)方法,其用來得到表達(dá)式的絕對值。二、絕對值的特性及其運(yùn)用:1、正數(shù)的絕對值是其本身。2、負(fù)數(shù)的絕對值是其相反數(shù)。3、零的絕對值是其本身。
推薦度:
標(biāo)簽: java 絕對值函數(shù)
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top