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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java简单通讯录的实现02person类_Java中Math类的简单介绍

發(fā)布時間:2025/4/5 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java简单通讯录的实现02person类_Java中Math类的简单介绍 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我想對于Math類大家一定很熟悉了,是Java提供的一個用來進行簡單數(shù)學運算的工具類。對于Math類來說,常用的方法有:

  • 加法
  • public static int addExact(int x, int y);求兩個int類型整數(shù)的和
  • public static long addExact(long x, long y):求兩個long類型整型數(shù)的和
  • 加法的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 加法 System.out.println("3 + 4 = " + Math.addExact(3, 4)); System.out.println("5L + 4L = " + Math.addExact(5L, 4L)); }}

    執(zhí)行結果如下圖所示:

    • 減法
  • public static int subtractExact(int x, int y):求兩個int類型整數(shù)的差
  • public static long subtractExact(long x, long y):求兩個long類型整數(shù)的差
  • 減法的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 減法 System.out.println("6 - 4 = " + Math.subtractExact(6, 4)); System.out.println("7L - 2L = " + Math.subtractExact(7L, 2L)); }}

    執(zhí)行結果如下圖所示:

    • 乘法
  • public static int multiplyExact(int x, int y):求兩個int類型整數(shù)的積
  • public static long multiplyExact(long x, int y):求一個long類型整型數(shù)和一個int類型整數(shù)的積
  • public static long multiplyExact(long x, long y):求兩個long類型整型數(shù)的積
  • 乘法的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 乘法 System.out.println("6 * 4 = " + Math.multiplyExact(6, 4)); System.out.println("7L * 2 = " + Math.multiplyExact(7L, 2)); System.out.println("7L * 5L = " + Math.multiplyExact(7L, 5L)); }}

    執(zhí)行結果如下圖所示:

    • 除法
  • public static int floorDiv(int x, int y):求兩個int類型整數(shù)的相除的結果
  • public static long floorDiv(long x, int y):求一個long類型整型數(shù)除以一個int類型整數(shù)的結果
  • public static long floorDiv(long x, long y):求兩個long類型整型數(shù)相除的結果
  • 除法的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 除法 System.out.println("6 / 4 = " + Math.floorDiv(6, 4)); System.out.println("7L / 2 = " + Math.floorDiv(7L, 2)); System.out.println("7L / 5L = " + Math.floorDiv(7L, 5L)); }}

    執(zhí)行結果如下圖所示:

    • 求余
  • public static int floorMod(int x, int y):求兩個int類型整數(shù)的取模
  • public static int floorMod(long x, int y):求一個long類型整型數(shù)對另一個int類型整數(shù)的取模
  • public static long floorMod(long x, long y):求兩個long類型整型數(shù)的取模
  • 求余的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 求余 System.out.println("6 % 4 = " + Math.floorMod(6, 4)); System.out.println("7L % 2 = " + Math.floorMod(7L, 2)); System.out.println("7L % 5L = " + Math.floorMod(7L, 5L)); }}

    執(zhí)行結果如下圖所示:

    • 取反
  • public static int negateExact(int a):對一個int類型的整數(shù)進行取反
  • public static long negateExact(long a):對一個long類型的整數(shù)進行取反
  • 取反的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 取反 System.out.println("3取反的結果為:" + Math.negateExact(3)); System.out.println("-5L取反的結果為:" + Math.negateExact(-5L)); }}

    執(zhí)行結果如下圖所示:

    • 取兩個數(shù)的最大數(shù)
  • public static int max(int a, int b):取兩個int類型整數(shù)的最大數(shù)
  • public static long max(long a, long b):取兩個long類型整數(shù)的最大數(shù)
  • public static float max(float a, float b):取兩個float類型浮點數(shù)的最大數(shù)
  • public static double max(double a, double b):取兩個double類型浮點數(shù)的最大數(shù)
  • 取兩個數(shù)的最大數(shù)的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 取兩個數(shù)的最大數(shù) System.out.println("3和4的最大數(shù)為:" + Math.max(3, 4)); System.out.println("9L和4L的最大數(shù)為:" + Math.max(9L, 4L)); System.out.println("9.3F和14.2F的最大數(shù)為:" + Math.max(9.3F, 14.2F)); System.out.println("9.3和4.2的最大數(shù)為:" + Math.max(9.3, 4.2)); }}

    執(zhí)行結果如下圖所示:

    • 取兩個數(shù)的最小數(shù)
  • public static int min(int a, int b):取兩個int類型整數(shù)的最小數(shù)
  • public static long min(long a, long b):取兩個long類型整數(shù)的最小數(shù)
  • public static float min(float a, float b):取兩個float類型浮點數(shù)的最小數(shù)
  • public static double min(double a, double b):取兩個double類型浮點數(shù)的最小數(shù)
  • 取兩個數(shù)的最小數(shù)為:

    public class MathTest { public static void main(String[] args) { // 取兩個數(shù)的最小數(shù) System.out.println("3和4的最小數(shù)為:" + Math.min(3, 4)); System.out.println("9L和4L的最小數(shù)為:" + Math.min(9L, 4L)); System.out.println("9.3F和14.2F的最小數(shù)為:" + Math.min(9.3F, 14.2F)); System.out.println("9.3和4.2的最小數(shù)為:" + Math.min(9.3, 4.2)); }}

    執(zhí)行結果如下圖所示:

    • 取絕對值:
  • public static int abs(int a):取一個int類型整數(shù)的絕對值
  • public static long abs(long a):取一個long類型整數(shù)的絕對值
  • public static float abs(float a):取一個float類型浮點數(shù)的絕對值
  • public static double abs(double a):取一個double類型浮點數(shù)的絕對值
  • 取絕對值的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 取絕對值 System.out.println("3的絕對值為:" + Math.abs(3)); System.out.println("-3L的絕對值為:" + Math.abs(-3L)); System.out.println("-3.5F的絕對值為:" + Math.abs(-3.5F)); System.out.println("9.3的絕對值為:" + Math.abs(9.3)); }}

    執(zhí)行結果如下圖所示:

    • 取隨機數(shù)
    public static double random():獲取一個0-1之間的隨機小數(shù)。

    取隨機數(shù)的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 取絕對值 for (int i = 0; i < 5; i++) { System.out.println("第" + (i + 1) + "次獲取的隨機數(shù)為:" + Math.random()); } }}

    執(zhí)行結果如下圖所示:

    • 自動加一
  • public static int incrementExact(int a):對一個int類型整數(shù)自動加1
  • public static long incrementExact(long a):對一個long類型整數(shù)自動加1
  • 自動加1的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 取絕對值 int intValue = 10; long longValue = 20; for (int i = 0; i < 5; i++) { intValue = Math.incrementExact(intValue); System.out.println("第" + (i + 1) + "次加1后的值為:" + intValue); longValue = Math.incrementExact(longValue); System.out.println("第" + (i + 1) + "次加1后的值為:" + longValue); } }}

    執(zhí)行結果如下圖所示:

    • 自動減一
  • public static int decrementExact(int a):對一個int類型整數(shù)自動減1
  • public static long decrementExact(long a):對一個long類型整數(shù)自動減1
  • 自動減一的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 取絕對值 int intValue = 10; long longValue = 20; for (int i = 0; i < 5; i++) { intValue = Math.decrementExact(intValue); System.out.println("第" + (i + 1) + "次減1后的值為:" + intValue); longValue = Math.decrementExact(longValue); System.out.println("第" + (i + 1) + "次減1后的值為:" + longValue); } }}

    執(zhí)行結果如下圖所示:

    • 向上取整
  • public static double ceil(double a):將一個浮點數(shù)向上取整(需要說明的是,由于float可以自動轉換為double,所以該方法可以滿足所有浮點數(shù)的向上取整的需求。當然,大多數(shù)時候,浮點數(shù)還是習慣于用double表示。)
  • 向上取整的示例如下所示:

    public class MathTest { public static void main(String[] args) { // 向上取整 System.out.println("3.0向上取整結果為:" + Math.ceil(3.0)); System.out.println("3.2向上取整結果為:" + Math.ceil(3.2)); System.out.println("3.5向上取整結果為:" + Math.ceil(3.5)); }}

    執(zhí)行結果如下圖所示:

    • 向下取整
  • public static double floor(double a):將一個浮點數(shù)向下取整。
  • 向下取整的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 向上取整 System.out.println("3.0向下取整結果為:" + Math.floor(3.0)); System.out.println("3.2向下取整結果為:" + Math.floor(3.2)); System.out.println("3.6向下取整結果為:" + Math.floor(3.6)); }}

    執(zhí)行結果如下圖所示:

    • 四舍五入
  • public static int round(float a):將一個float類型的浮點數(shù)進行四舍五入。
  • public static long round(double a):將一個double類型的浮點數(shù)進行四舍五入。
  • public static double rint(double a):將一個double類型的浮點數(shù)進行四舍五入。無需說明的是,當該浮點數(shù)距兩側的整數(shù)差值相等的時候,會優(yōu)先向偶數(shù)靠攏。
  • 四舍五入的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 四舍五入 System.out.println("3.0四舍五入結果為:" + Math.round(3.0)); System.out.println("3.2四舍五入結果為:" + Math.round(3.2F)); System.out.println("3.6四舍五入結果為:" + Math.round(3.6)); }}

    執(zhí)行結果如下圖所示:

    • 判斷一個數(shù)的正負(若返回值是1.0則為正數(shù),若返回值是-1.0為負數(shù))
  • public static double signum(double d):判斷double類型浮點數(shù)是正數(shù)還是負數(shù)
  • public static float signum(float f):判斷float類型的浮點數(shù)是正數(shù)還是負數(shù)。
  • 判斷一個數(shù)的正負的代碼示例如下所示:

    public class MathTest { public static void main(String[] args) { // 判斷數(shù)的正負 System.out.println("3是否為正數(shù):" + (Math.signum(3) > 0)); System.out.println("3是否為負數(shù):" + (Math.signum(3) < 0)); System.out.println("-3是否為正數(shù):" + (Math.signum(-3) > 0)); System.out.println("-3是否為負數(shù):" + (Math.signum(-3) < 0)); }}

    執(zhí)行結果如下圖所示:

    • 開方
  • public static double pow(double a, double b):求a的b次方的值
  • public static double exp(double a):求e的a次方的值
  • public static double expm1(double x):求e的x次方加1的值
  • 開方的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 開方 System.out.println("3的4次方為:" + Math.pow(3, 4)); System.out.println("2的5次方為:" + Math.pow(2, 5)); System.out.println("e的2次方為:" + Math.exp(2)); System.out.println("e的2次方減1為:" + Math.expm1(2)); }}

    執(zhí)行結果如下圖所示:

    • 開根
  • public static double sqrt(double a):求double類型浮點數(shù)的平方根
  • public static double cbrt(double a):求double類型浮點數(shù)的立方根
  • 開根的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 開根 System.out.println("4的平方根方為:" + Math.sqrt(4)); System.out.println("9的平方根方為:" + Math.sqrt(9)); System.out.println("8的立方根方為:" + Math.cbrt(8)); System.out.println("27的立方根方為:" + Math.cbrt(27)); }}

    執(zhí)行結果如下圖所示:

    • 求對數(shù)
  • public static double log(double a):求以e為底數(shù)求double類型浮點數(shù)的對數(shù)。
  • public static double log10(double a):求以10為底數(shù)求double類型浮點數(shù)的對數(shù)
  • public static double log1p(double x):求以e為底傳入double類型浮點數(shù)加1之后的對數(shù)。
  • 求對數(shù)的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 求對數(shù) System.out.println("e以e為底的對數(shù)為:" + Math.log(Math.E)); System.out.println("100以10為底的對數(shù)為:" + Math.log10(100)); System.out.println("10加1之后以e為底的對數(shù)為:" + Math.log1p(10)); System.out.println("e - 1加1之后以e為底的對數(shù)為:" + Math.log1p(Math.E - 1)); }}

    執(zhí)行結果如下圖所示:

    • 復制正負號
  • public static double copySign(double magnitude, double sign):根據第一個double類型浮點數(shù)的絕對值加第二個double類型浮點數(shù)的正負得出最終結果。
  • public static float copySign(float magnitude, float sign):根據第一個float類型浮點數(shù)的絕對值加第二個float類型浮點數(shù)的正負得出最終結果。
  • 復制正負號的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 以第一個參數(shù)的絕對值為值,以第二個參數(shù)的符號位符號 System.out.println("(4.0F, -1.0F)執(zhí)行copySign方法得到的結果為:" + Math.copySign(4.0F, -1.0F)); System.out.println("(-4.0F, -1.0F)執(zhí)行copySign方法得到的結果為:" + Math.copySign(-4.0F, -1.0F)); System.out.println("(4.0, 1.0)執(zhí)行copySign方法得到的結果為:" + Math.copySign(4.0, 1.0)); System.out.println("(-4.0, 1.0)執(zhí)行copySign方法得到的結果為:" + Math.copySign(-4.0, 1.0)); }}

    執(zhí)行結果如下圖所示:

    • 求正弦、余弦、正切、反正弦、反余弦和反正切
  • public static double sin(double a):求正弦
  • public static double cos(double a):求余弦
  • public static double tan(double a):求正切
  • public static double asin(double a):求反正弦
  • public static double acos(double a):求反余弦
  • public static double atan(double a):求反正切
  • public static double sinh(double x) :求雙曲正弦
  • public static double cosh(double x) :求雙曲余弦
  • public static double tanh(double x):求雙曲正切
  • 求正弦、余弦、正切、反正弦、反余弦、反正切、雙曲正弦、雙曲余弦、雙曲正切的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 正弦 System.out.println(Math.sin(Math.PI / 4)); // 余弦 System.out.println(Math.cos(Math.PI / 4)); // 正切 System.out.println(Math.tan(Math.PI / 4)); // 反正弦 System.out.println(Math.asin(Math.PI / 4)); // 反余弦 System.out.println(Math.acos(Math.PI / 4)); // 反正切 System.out.println(Math.atan(Math.PI / 4)); // 雙曲正弦 System.out.println(Math.sinh(Math.PI / 4)); // 雙曲余弦 System.out.println(Math.cosh(Math.PI / 4)); // 雙曲正切 System.out.println(Math.tanh(Math.PI / 4)); }}

    執(zhí)行結果如下圖所示:

    • 角度和弧度互轉
  • public static double toDegrees(double angrad):將弧度轉為角度
  • public static double toRadians(double angdeg):將角度轉弧度
  • 角度和弧度互轉的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 角度轉弧度 System.out.println(Math.toRadians(45)); // 弧度轉角度 System.out.println(Math.toDegrees(Math.PI / 2)); }}

    執(zhí)行結果如下圖所示:

    • 求直角三角形的斜邊
    public static double hypot(double x, double y):求直角三角形的斜邊,即對x的平方加y的平方的和開根

    求直角三角形的斜邊的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 若兩直角邊為3和4,則斜邊為5 System.out.println(Math.hypot(3, 4)); }}

    執(zhí)行結果如下圖所示:

    ?

    • 求前兩個數(shù)相乘后與第三個數(shù)相加的和
    public static float fma(float a, float b, float c):結果相當與a * b + c;

    求前兩個數(shù)相乘后與第三個數(shù)相加的和的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 求前兩個數(shù)相乘后與第三個數(shù)相加的和 System.out.println("3 * 4 + 5 = " + Math.fma(3, 4, 5)); }}

    執(zhí)行結果如下圖所示:

    • 返回比目標值略大或略小一點的浮點數(shù)
  • public static double nextUp(double d) :返回一個比double類型浮點數(shù)略大一點的double類型的浮點數(shù)
  • public static float nextUp(float f):返回一個比float類型浮點數(shù)略大一點的float類型的浮點數(shù)
  • public static double nextDown(double d):返回一個比double類型浮點數(shù)略小一點的double類型的浮點數(shù)
  • public static float nextDown(float f):返回一個比float類型浮點數(shù)略小一點的float類型的浮點數(shù)
  • public static double nextAfter(double start, double direction):返回一個在兩個double類型浮點數(shù)間比第一個double類型浮點數(shù)略大一點的浮點數(shù)。
  • public static float nextAfter(float start, double direction):返回一個在兩個float類型浮點數(shù)間比第一個float類型浮點數(shù)略小一點的浮點數(shù)
  • 返回比目標值略大或略小一點的浮點數(shù)的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // 求前兩個數(shù)相乘后與第三個數(shù)相加的和 System.out.println("比1.0F略大的小數(shù)為:" + Math.nextUp(1.0F)); System.out.println("比1.2略大的小數(shù)為:" + Math.nextUp(1.2)); System.out.println("比1.0F略小的小數(shù)為:" + Math.nextDown(1.0F)); System.out.println("比1.2F略小的小數(shù)為:" + Math.nextDown(1.2)); System.out.println("在1.1F和1.2F之間靠近1.1F的小數(shù)為:" + Math.nextAfter(1.1F, 1.2F)); System.out.println("在1.5和1.6之間靠近1.5的小數(shù)為:" + Math.nextAfter(1.5, 1.6)); }}

    執(zhí)行結果如下圖所示:

    • ??第一個參數(shù)和2的第二個參數(shù)次方的積。
  • public static float scalb(float f, int scaleFactor):表示二進制第scaleFactor位的值為f時對應的十進制的值。
  • public static double scalb(double d, int scaleFactor):表示二進制第scaleFactor位的值為d時對應的十進制的值。
  • ??計算二進制的某一位對應的值的示例代碼如下所示:

    public class MathTest { public static void main(String[] args) { // System.out.println("3 * 2的4次方的結果為:" + Math.scalb(3, 4)); System.out.println("4 * 2的4次方的結果為:" + Math.scalb(4, 4)); }}

    執(zhí)行結果如下圖所示:

    除此以外,還有兩個特殊的常量值:Math.PI和Math.E。

    總結

    以上是生活随笔為你收集整理的java简单通讯录的实现02person类_Java中Math类的简单介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

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