第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

隨機枚舉返回空

隨機枚舉返回空

達令說 2021-06-07 17:24:39
您好,我正在嘗試獲取隨機 ENUM,但它返回 null,有人可以幫助我我的代碼有什么問題嗎?我試圖獨自修理它,但我放棄了。public class LotteryMachine {    protected enum Sings {        ONE,        TWO,        THREE}    private static final List<Sings> SINGS_LIST = Collections.unmodifiableList(Arrays.asList(Sings.values()));    private static final int SIZE = SINGS_LIST.size();    private static final Random RANDOM = new Random();    Sings randomSing() {        return SINGS_LIST.get(RANDOM.nextInt(SIZE));    }}public class Game {    private LotteryMachine lotteryMachine = new LotteryMachine();    private LotteryMachine.Sings singOne;    private LotteryMachine.Sings singTwo;    private LotteryMachine.Sings singThree;    private void Lottery(){        this.singOne = lotteryMachine.randomSing();        this.singTwo = lotteryMachine.randomSing();        this.singThree = lotteryMachine.randomSing();    }    public void viewLottery(){        System.out.print(singOne + " " + singTwo + " " + singThree);    }}
查看完整描述

1 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

你的問題似乎在這里


private void Lottery(){

    this.singOne = lotteryMachine.randomSing();

    this.singTwo = lotteryMachine.randomSing();

    this.singThree = lotteryMachine.randomSing();

}

這不是構造函數(shù);相反,它是一個Lottery()被Game類調用但不使用的私有方法。因為從未調用該方法,所以所有singX字段都未初始化(即為空)。將初始化邏輯從所述方法移動到像這樣的字段聲明后,事情應該可以正常工作


private LotteryMachine.Sings singOne = lotteryMachine.randomSing();

private LotteryMachine.Sings singTwo = lotteryMachine.randomSing();

private LotteryMachine.Sings singThree = lotteryMachine.randomSing();

或者聲明Game類的構造函數(shù)如下:


public Game() {

    this.singOne = lotteryMachine.randomSing();

    this.singTwo = lotteryMachine.randomSing();

    this.singThree = lotteryMachine.randomSing();

}


查看完整回答
反對 回復 2021-06-10
  • 1 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號