java基础 大数类

BigInteger和BigDecimal是util包下面的两个类。

他们能处理任意长度的整数和实数。

静态方法valueof()能把primitive转换成对应的大数对象。

加减乘除必须使用对应对象的方法。

两个大数比较要使用compareto方法。

public static void main(String[] args) {
        Scanner in  = new Scanner(System.in);

        System.out.println("how many numbers do you need to draw?");
        int k = in.nextInt();

        System.out.println("What is the highest number you can draw?");
        int n = in.nextInt();


        BigInteger lotteryOdds = BigInteger.valueOf(1);

        for(int i = 1; i <= k; i ++) {
            lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n-i+1) . divide(BigInteger.valueOf(i)));
        }
        System.out.println(lotteryOdds);
    }
原文地址:https://www.cnblogs.com/custoyth/p/13366180.html