當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.StrictMath.abs()用法及代碼示例


描述

這個java.lang.StrictMath.abs(int a) 方法返回一個 int 值的絕對值。如果參數不是負數,則返回該參數。如果參數為負,則返回參數的否定。

聲明

以下是聲明java.lang.StrictMath.abs()方法

public static int abs(int a)

參數

a─ 這是要確定絕對值的參數。

返回值

此方法返回 int 值的絕對值。

異常

NA

示例

下麵的例子展示了 java.lang.StrictMath.abs() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

   public static void main(String[] args) {

      int i1 = 54 , i2 = -35;
     
      // returns the absolute value of positive int value
      int iAbsValue = StrictMath.abs(i1);
      System.out.println("absolute value of " + i1 + " = " + iAbsValue);
   
      // returns the absolute value of negative int value
      iAbsValue = StrictMath.abs(i2);
      System.out.println("absolute value of " + i2 + " = " + iAbsValue);
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

absolute value of 54 = 54
absolute value of -35 = 35

相關用法


注:本文由純淨天空篩選整理自 Java.lang.StrictMath.abs() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。