亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何更改方法的多個傳遞變量,然后在Java中返回所述變量?

如何更改方法的多個傳遞變量,然后在Java中返回所述變量?

喵喵時光機 2022-08-03 10:50:40
我有一個看起來像這樣的方法...public static ArrayList<Integer> bettingCycle(    ArrayList<Integer> playersRemaining,     String[][] playerHands,    ArrayList<Integer> bets,     int totalPot) {     //irrelevant code    return playersRemaining;}我的方法成功返回調整后的 ArrayList 玩家恢復,但不會更改任何傳入的變量(玩家手牌、投注、總池)。為了使我的程序正常工作,我需要以某種方式調整玩家手牌,賭注和變量中傳遞的總池。無論我嘗試了什么,我仍然無法得到任何東西,除了返回的改變。playersRemaining為了幫助解釋我希望完成的工作,這里有一個示例,其中包含注釋以幫助解釋我的問題。public static ArrayList<Integer> playHand(   ArrayList<Integer> initialPlayers,    String[][] initialPlayerHands,    ArrayList<Integer> preliminaryBets //assumed to be initially 0's   int initialPot //also assumed to be initially 0)   {   //fancy code that allows for initial betting cycle (so that the values    //passed into the bettingCycle method are not null, or invalid   //It is here that the values are initially changed before passing to the    //bettingCycle    if (!allPlayersCalled) {       bettingCycle(updatedPlayersRemaining, updatedPlayerHands,        updatedBets, updatedTotalPot);        //when I call this, only the        //updatedPlayersRemaining changes,        //but none of the other variables.        //I'd like for bettingCycle() to also change the other passed in        //variable, so I can use the changed even further along in the method  }  return updatedPlayersRemaining;}關于如何改變傳遞的變量,以便可以適當地調整它們的任何想法?
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

我建議使用DTO類,其中包含可以作為屬性修改的參數


public class CycleDTO {


    private String[][] playerHands,

    private List<Integer> bets, 

    private int totalPot

    List<Integer> playersRemaining, 


    // getters and setters

}

修改投注周期方法以接收dto


public static void bettingCycle( CycleDTO  dto) {

  // "irrelevant code"

}

修改調用以傳遞 dto


...

if (!allPlayersCalled) {

    CycleDTO dto = new CycleDTO();

    dtp.setPlayersRemaininf(updatedPlayersRemaining)

    dto.setPlayerHands(updatedPlayerHands);

    dto.setBets(updatedBets);

    dto.setTotalPot(updatedTotalPot);


    bettingCycle(dto); 


...


查看完整回答
反對 回復 2022-08-03
  • 1 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號