日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java面试题33 Math.round(11.5) 等于多少 (). Math.round(-11.5) 等于多少 ( ).

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java面试题33 Math.round(11.5) 等于多少 (). Math.round(-11.5) 等于多少 ( ). 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java面試題33 Math.round(11.5) 等于多少 (). Math.round(-11.5) 等于多少 (? ).

?

A 11 ,-11 B 11 ,-12 C 12 ,-11 D 12 ,-12

蒙蔽樹上蒙蔽果,蒙蔽樹下你和我。

做Java的面試題時遇到了以下這題,百度了一下Math.round()的修約規則,有的說是四舍五入,有的說是四舍六入,發現和我學分析化學時用的數字修約規則(四舍六入五成雙)很像,所以驗證一下;
原題:Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?
作者給的解題方法如下:
答:Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在參數上加0.5然后進行下取整。

先說結論,題目作者給的解釋是對的,后來找了該方法的定義,結果方法的定義就是這個原理,果然看文檔才是王道;

round方法:
static long round(double a)
此方法返回的參數最接近的long.
static int round(float a)
此方法返回的參數最接近的整數.

注:四舍六入五成雙:
當有效位數確定后,其后面多余的數字應該舍去,只保留有效數字最末一位,這種修約(舍入)規則是“四舍六入五成雙”,也即“4舍6入5湊偶”這里“四”是指≤4 時舍去,”六”是指≥6時進上,”五”指的是根據5后面的數字來定,當5后有數時,舍5入1;當5后無有效數字時,需要分兩種情況來講:①5前為奇數,舍5入1;②5前為偶數,舍5不進。(0是偶數)

以下只論證static int round(float a)

??????? //四舍
???

int[] test1 = {Math.round(2.40f),Math.round(2.44f),Math.round(2.45f),Math.round(2.46f),Math.round(-2.40f),Math.round(-2.44f),Math.round(-2.45f),Math.round(-2.46f),Math.round(3.40f),Math.round(3.44f),Math.round(3.45f),Math.round(3.46f),Math.round(-3.40f),Math.round(-3.44f),Math.round(-3.45f),Math.round(-3.46f)};for(int i = 0; i< test1.length; i++){ System.out.print(test1[i]+",");}


??????? //輸出:2,2,2,2,-2,-2,-2,-2,3,3,3,3,-3,-3,-3,-3,符合四舍;也符合 加0.5,進行下取整;

??????? //六入
???????

int[] test2 = {Math.round(2.60f),Math.round(2.64f),Math.round(2.65f),Math.round(2.66f),Math.round(-2.60f),Math.round(-2.64f),Math.round(-2.65f),Math.round(-2.66f),Math.round(3.60f),Math.round(3.64f),Math.round(3.65f),Math.round(3.66f),Math.round(-3.60f),Math.round(-3.64f),Math.round(-3.65f),Math.round(-3.66f)};for(int i = 0; i< test2.length; i++){ System.out.print(test2[i]+",");}


??????? //輸出:3,3,3,3,-3,-3,-3,-3,4,4,4,4,-4,-4,-4,-4,符合六入;也符合 加0.5,進行下取整;

??????? //五成雙之五后無數字
??????

int[] test3 = {Math.round(2.5f),Math.round(-2.5f),Math.round(3.5f),Math.round(-3.5f)};for(int i = 0; i< test3.length; i++){ System.out.print(test3[i]+",");}


??????? //輸出:3,-2,4,-3,不符合五成雙;符合 加0.5,進行下取整;

??????? //五成雙之五后有數字(零,非零)
???

int[] test4 = {Math.round(2.50f),Math.round(2.51f),Math.round(2.59f), Math.round(-2.50f),Math.round(-2.51f),Math.round(-2.59f),Math.round(3.50f),Math.round(3.51f),Math.round(3.59f), Math.round(-3.50f),Math.round(-3.51f),Math.round(-3.59f),};for(int i = 0; i< test4.length; i++){ System.out.print(test4[i]+",");}


??????? //輸出:3,3,3,-2,-3,-3,4,4,4,-3,-4,-4,不符合五后非零進一;符合 加0.5,進行下取整;

??????? //結論:Math.round()的取整規則不符合四舍六入五成雙,以上案例符合 加0.5,進行下取整;

答案C

我是歌謠,如有不合理之處歡迎指出。喜歡敲代碼,閑時刷刷題。

閱讀目錄(置頂)(長期更新計算機領域知識)

閱讀目錄(置頂)(長期更新計算機領域知識)

閱讀目錄(置頂)(長期科技領域知識)

歌謠帶你看java面試題

總結

以上是生活随笔為你收集整理的java面试题33 Math.round(11.5) 等于多少 (). Math.round(-11.5) 等于多少 ( ).的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。