Java - StrictMath min(int x, int y) 方法



描述

Java StrictMath min(int a, int b) 方法返回兩個 int 值中較小的一個。也就是說,結果是更接近負無窮大的值。如果引數具有相同的值,則結果為該相同的值。如果任一值為 NaN,則結果為 NaN。與數值比較運算子不同,此方法認為負零嚴格小於正零。如果一個引數為正零,另一個引數為負零,則結果為負零。

宣告

以下是 java.lang.StrictMath.min() 方法的宣告

public static int min(int a,  int b)

引數

  • a − 一個引數

  • b − 另一個引數

返回值

此方法返回 a 和 b 中較小的一個。

異常

獲取兩個正整數的最小值示例

以下示例演示了兩個正值的 StrictMath min() 方法的使用。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two int numbers
      int x = 60984;
      int y = 497;
   
      // call min and print the result
      System.out.println("StrictMath.min(" + x + "," + y + ")=" + StrictMath.min(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

StrictMath.min(60984,497)=497

獲取一個正整數和一個負整數的最小值示例

以下示例演示了正值和負值的 StrictMath min() 方法的使用。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two int numbers
      int x = -60984;
      int y = 497;
   
      // call min and print the result
      System.out.println("StrictMath.min(" + x + "," + y + ")=" + StrictMath.min(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

StrictMath.min(-60984,497)=-60984

獲取兩個負整數的最小值示例

以下示例演示了負值的 StrictMath min() 方法的使用。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two int numbers
      int x = -60984;
      int y = -497;
   
      // call min and print the result
      System.out.println("StrictMath.min(" + x + "," + y + ")=" + StrictMath.min(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

StrictMath.min(-60984,-497)=-60984
java_lang_strictmath.htm
廣告

© . All rights reserved.