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


Java BigInteger compareTo()用法及代碼示例


java.math.BigInteger.compareTo(BigInteger value)方法將此BigInteger與作為參數傳遞的BigInteger進行比較。

用法:

public int compareTo(BigInteger val)

參數:此方法接受單個強製性參數val,該參數為BigInteger以便與BigInteger對象進行比較。


返回值:此方法返回以下內容:

  • 0:如果此BigInteger的值等於作為參數傳遞的BigInteger對象的值。
  • 1:如果此BigInteger的值大於作為參數傳遞的BigInteger對象的值。
  • -1:如果此BigInteger的值小於作為參數傳遞的BigInteger對象的值。

例子:

Input: BigInteger1=2345, BigInteger2=7456
Output: -1
Explanation: BigInteger1.compareTo(BigInteger2)=-1.

Input: BigInteger1=9834, BigInteger2=7456
Output: 1
Explanation: BigInteger1.compareTo(BigInteger2)=1.

示例1:以下示例程序旨在說明兩個BigInteger相等時BigInteger類的compareTo()方法

// Java program to demonstrate  
// compareTo() method of BigInteger 
  
import java.math.BigInteger; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // Creating 2 BigInteger objects 
        BigInteger b1, b2; 
  
        b1 = new BigInteger("321456"); 
        b2 = new BigInteger("321456"); 
  
        // apply compareTo() method 
        int comparevalue = b1.compareTo(b2); 
  
        // print result 
        if (comparevalue == 0) { 
  
            System.out.println("BigInteger1 "
                               + b1 + " and BigInteger2 "
                               + b2 + " are equal"); 
        } 
        else if (comparevalue == 1) { 
  
            System.out.println("BigInteger1 " + b1 + "  
                is greater than BigInteger2 " + b2); 
        } 
        else { 
  
            System.out.println("BigInteger1 " + b1 + "  
                is lesser than BigInteger2 " + b2); 
        } 
    } 
}
輸出:
BigInteger1 321456 and BigInteger2 321456 are equal

示例2:當BigInteger1大於BigInteger2時

// Java program to demonstrate  
// compareTo() method of BigInteger 
  
import java.math.BigInteger; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // Creating 2 BigInteger objects 
        BigInteger b1, b2; 
  
        b1 = new BigInteger("654321"); 
        b2 = new BigInteger("321456"); 
  
        // apply compareTo() method 
        int comparevalue = b1.compareTo(b2); 
  
        // print result 
        if (comparevalue == 0) { 
  
            System.out.println("BigInteger1 " + b1 + "  
                and BigInteger2 " + b2 + " are equal"); 
        } 
        else if (comparevalue == 1) { 
  
            System.out.println("BigInteger1 " + b1 + " 
                is greater than BigInteger2 " + b2); 
        } 
        else { 
  
            System.out.println("BigInteger1 " + b1 + "  
                is lesser than BigInteger2 " + b2); 
        } 
    } 
}
輸出:
BigInteger1 654321 is greater than BigInteger2 321456

示例3:當BigInteger1小於BigInteger2時

// Java program to demonstrate  
// compareTo() method of BigInteger 
  
import java.math.BigInteger; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // Creating 2 BigInteger objects 
        BigInteger b1, b2; 
  
        b1 = new BigInteger("321456"); 
        b2 = new BigInteger("564321"); 
  
        // apply compareTo() method 
        int comparevalue = b1.compareTo(b2); 
  
        // print result 
        if (comparevalue == 0) { 
  
            System.out.println("BigInteger1 " + b1 + "  
                 and BigInteger2 " + b2 + " are equal"); 
        } 
        else if (comparevalue == 1) { 
  
            System.out.println("BigInteger1 " + b1 + "  
                  is greater than BigInteger2 " + b2); 
        } 
        else { 
  
            System.out.println("BigInteger1 " + b1 + " 
                 is lesser than BigInteger2 " + b2); 
        } 
    } 
}
輸出:
BigInteger1 321456 is lesser than BigInteger2 564321

參考:
BigInteger CompareTo() Docs



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger compareTo() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。