java biginteger 比较大小,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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值