1

変数を long として定義しましたが、それを配列で使用しようとすると、値が int の範囲外であるというエラーがスローされ続けます。冗談ではありません。これは長いので、1 つと定義しました。

以下は私のコードです。2 番目のクラスである LoanOfficer には、2 番目の申請者である Bill Gates が見つかり、年収は 3,710,000,000 で、エラーがスローされています。

public class Applicant {
    private String name;
    private int creditScore;
    private long annualIncome;
    private int downPayment;
    private boolean status;

    public Applicant(String name, int creditScore, long annualIncome,
            int downPayment) {
        this.name = name;
        this.creditScore = creditScore;
        this.annualIncome = annualIncome;
        this.downPayment = downPayment;
        this.status = false;
    }

    public String getName() {
        return name;
    }

    public int getCreditScore() {
        return creditScore;
    }

    public long getAnnualIncome() {
        return annualIncome;
    }

    public int getDownPayment() {
        return downPayment;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public boolean isStatus() {
        return status;
    }
}

public class LoanOfficer {
    public static void main(String[] args) {
        Applicant[] applicants = {
                new Applicant("MC Hammer", 400, 25000, 5000),
                new Applicant("Bill Gates", 850, 3710000000, 500000),
                new Applicant("MC Hammer", 400, 25000, 5000),
                new Applicant("MC Hammer", 400, 25000, 5000), };
    }
}
4

3 に答える 3

15

Llong として扱われる数値には接尾辞が必要です。

new Applicant("Bill Gates", 850, 3710000000L, 500000)

サフィックスが欠落している場合L、コンパイラはリテラルをint.

于 2013-04-08T19:20:37.173 に答える
5

を追加して、長いリテラルとして 3710000000 を指定する必要がありますL

new Applicant("Bill Gates", 850, 3710000000L, 500000),

JLSから

整数リテラルは、ASCII 文字の L または l (エル) の接尾辞が付いている場合、long 型です。それ以外の場合は int 型です

于 2013-04-08T19:20:36.530 に答える