我已将变量定义为long,当我尝试在数组中将其用作变量时,它会不断抛出错误,指出我的值超出了int范围。好吧,不是在开玩笑,它很长,我把它定义为一个。

下面是我的代码。在第二类LoanOfficer中,您将找到第二个申请人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), };
    }
}

最佳答案

您需要在被视为多头的数字上使用L后缀:

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


如果缺少L后缀,则编译器会将文字视为int

09-26 06:28