热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java中的BigIntegercompareTo()方法

Java中的BigIntegercompareTo()方法

Java 中的 BigInteger compareTo()方法

原文:https://www . geesforgeks . org/big integer-compare to-method-in-Java/

Java . math . BigInteger . compare to(BigInteger 值)方法将此 BigInteger 与作为参数传递的 BigInteger 进行比较。

语法:

public int compareTo(BigInteger val)

参数:该方法接受单个强制参数,它是要与大整数对象进行比较的大整数。

返回:该方法返回以下内容:


  • 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 相等时 big integer 类的 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);
        }
    }
}

Output:

BigInteger1 321456 and BigInteger2 321456 are equal

例 2:当大整数 1 大于大整数 2 时

// 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);
        }
    }
}

Output:

BigInteger1 654321 is greater than BigInteger2 321456

例 3:当大整数 1 小于大整数 2 时

// 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);
        }
    }
}

Output:

BigInteger1 321456 is lesser than BigInteger2 564321

参考:T2大整数比较()文档


推荐阅读
author-avatar
520TING小妖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有