Java大数之大数比大小compareTo()和equals()的用法

import java.math.*;
import java.util.*;
public class Main {
    public static void main(String []args){
        Scanner cin = new Scanner (System.in);
        BigInteger a,b,c,d;
        a=cin.nextBigInteger();
        b=cin.nextBigInteger();
        c=cin.nextBigInteger();
        d=cin.nextBigInteger();
        if(a.compareTo(b)<0)//此时由于a<b,则返回值小于0;
        {
            System.out.println("a<b");
        }
        if(b.equals(c))//此时由于b==c,则返回值等于0;
        {
            System.out.println("b==c");
        }
        if(d.compareTo(c)>0)//此时由于d>c,则返回值大于0; 
        {
            System.out.println("d>c");
        }
    }
}
import java.util.Scanner;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        BigInteger a, b;
        BigInteger t = BigInteger.valueOf(0);
        a = cin.nextBigInteger();
        b = cin.nextBigInteger();
        while(!(a.equals(t)&&b.equals(t))){
            if(a.equals(b)){
                System.out.println("a==b");
            }
            else if(a.compareTo(b)>0){
                System.out.println("a>b");
            }
            else System.out.println("a<b");
            a = cin.nextBigInteger();
            b = cin.nextBigInteger();
        }
    }
} 

仅代表个人观点,欢迎交流探讨,勿喷~~~
Java大数之大数比大小compareTo()和equals()的用法_第1张图片

PhotoBy:WLOP

http://weibo.com/wlop

你可能感兴趣的:(java)