发布网友 发布时间:2022-05-07 23:00
共1个回答
热心网友 时间:2023-08-01 23:24
compareTo方法来比较,小于则返回-1,等于则返回0,大于则返回1
BigInteger a1 = new BigInteger("1");
BigInteger a2 = new BigInteger("2");
a1.compareTo(a2);
下面的例子显示math.BigInteger.compareTo()方法的用法
package com.yii;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