biginteger判断相等_java biginteger怎么比较大小

博客介绍了使用compareTo方法比较BigInteger对象的大小,该方法小于返回 -1,等于返回 0,大于返回 1。还给出了具体代码示例,通过比较两个BigInteger对象的值,并根据返回结果输出相应信息。

展开全部

compareTo方法来比较,小e68a84e8a2ad62616964757a686964616f31333365643662于则返回-1,等于则返回0,大于则返回1

BigInteger a1 = new BigInteger("1");

BigInteger a2 = new BigInteger("2");

a1.compareTo(a2);

下面的例子显示math.BigInteger.compareTo()方法的用法

package com.yiibai;import java.math.*;public class BigIntegerDemo {public static void main(String[] args) {

// create 2 BigInteger objects

BigInteger bi1, bi2;

bi1 = new BigInteger("6");

bi2 = new BigInteger("3");

// create int object

int res;

// compare bi1 with bi2

res = bi1.compareTo(bi2);

String str1 = "Both values are equal ";

String str2 = "First Value is greater ";

String str3 = "Second value is greater";

if( res == 0 )

System.out.println( str1 );

else if( res == 1 )

System.out.println( str2 );

else if( res == -1 )

System.out.println( str3 );

}}

让我们编译和运行上面的程序,这将产生以下结果:

First Value is greater

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值