当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。