本文介绍了比较数组中的大整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数组中找到3个最大的BigInteger对象?这是我的代码.

How to find 3 the biggest BigInteger objects in array? This is my code for now.

package masivi;

import java.math.BigInteger;
import java.util.Scanner;

public class largest3Numbers {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        BigInteger[] numbers = new BigInteger[n];
        BigInteger tempbiggest1 = new BigInteger("0");
        BigInteger biggest1 = new BigInteger("0");
        BigInteger tempbiggest2 = new BigInteger("0");
        BigInteger biggest2 = new BigInteger("0");
        BigInteger tempbiggest3 = new BigInteger("0");
        BigInteger biggest3 = new BigInteger("0");

        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = scan.nextBigInteger();
            if (numbers[i] > tempbiggest1) {

            }
        }
    }
}

推荐答案

这将无法编译:

if (numbers[i] > tempbiggest1) {

不能在BigInteger对象上使用>运算符.使用 compareTo 代替:

You cannot use the > operator on BigInteger objects. Use compareTo instead:

// Check if numbers[i] is larger than tempbiggest1
if (numbers[i].compareTo(tempbiggest1) > 0) {

这篇关于比较数组中的大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 20:03