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

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

將變量從一種方法傳遞到另一種方法?

將變量從一種方法傳遞到另一種方法?

ibeautiful 2023-10-13 16:43:37
假設我正在創建一副紙牌游戲,我的類中有兩種稱為 shuffle 和 randomInt 的方法。private static void shuffle(Card [] cardArray) {    for (int i = 1; i <= 20; s++) {        Card temp = cardArray[a];        cardArray[a] = cardArray[b];        cardArray[b] = temp;    }}private static void randomInt(int a, int b) {       a = (int)(Math.random() * 12);    b = (int)(Math.random() * 12);}這里的問題是如何將變量a和b方法傳遞randomInt()到shuffle()方法中?我知道我可以簡單地將其放入其中randomInt(),shuffle()它會正常工作,但我想知道是否有任何方法可以這樣做。我也很感謝有人解釋這個概念,因為我對 OOP 還很陌生。謝謝。
查看完整描述

5 回答

?
九州編程

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

讓您randomInt()返回數字并調用 中的函數shuffle()。


private static void shuffle(Card[] cardArray) {

    for (int i = 1; i <= 20; i++) {

        int a = randomInt();

        int b = randomInt();

        Card temp = cardArray[a];

        cardArray[a] = cardArray[b];

        cardArray[b] = temp;

    }

}


private static int randomInt() {   

    return (int)(Math.random() * 12);

}

這將根據randomInt()生成索引的方式洗牌你的牌組


查看完整回答
反對 回復 2023-10-13
?
守著一只汪

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

你可以創建一個 Deck 類并將你的邏輯和數據放在這個類中


public class Deck {

    private Card[] deck = new Card[52];

    public Deck(){

        initDeck();

    }

    public void shuffle() {

        for (int i = 1; i <= 20; i++)

        {

            int a = (int)(Math.random() * 12);

            int b = (int)(Math.random() * (52 - 12));

            swap(a,b);

        }

    }

    private void swap(int a,int b){

        Card temp = deck[a];

        deck[a] = deck[b];

        deck[b] = temp;

    }

    private void print() {

       ...

    }


}

在你的主要方法中做類似的事情


Deck d = new Deck();

deck.shuffle();

deck.print();


查看完整回答
反對 回復 2023-10-13
?
慕碼人2483693

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

在java中,該方法無法按您的預期工作(https://www.google.com/?q=java+call+by+value ... https://stackoverflow.com/a/40523/592355),但你可以解決:


您可以將輸出變量封裝在一個對象內(該修改將在方法退出后持續存在):



class TwoInts {

   int a,b;

}

和:


private static void randomInt(TwoInts container) {

  assert(conatiner != null);   

  container.a = (Math.random() * 12);

  container.b = (Math.random() * 12);

}

最直接的方法是(編寫一個具有一個返回值的方法):


 private static int rand(int offset, int max) {

    return (int) (Math.random() * max) + offset;

 }

..并調用它兩次:


 a = rand(0, 12);

 b = rand(0, 12);

...


還請您看一下java.util.Random...


查看完整回答
反對 回復 2023-10-13
?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

如果您希望應用程序從 randomInt 方法返回兩個值 a 和 b,則不能僅將 a 和 b 聲明為參數。java中的方法參數是“ByValue”參數。該方法不會更改調用者的 a 和 b 值。


首選選項:


讓 randomInt 返回一個包含兩個元素的數組。調用 randomInt 后,您可以將數組中的值分配給調用方方法中的變量 a 和 b。


替代選項,通過數組進行偽引用:


不要傳遞 a 和 b,而是將一個只有一個元素的數組傳遞給您的方法:


private static void randomInt(int[] a, int[] b)

  //assuming a[] and b[] both are an array with just one element

  //set a[0] and b[0] here like you already set a and b

}

在呼叫方,


...

int[] a = new int[1];

int[] b = new int[1];

randomInt(a, b);


//now you have your values in a[0] and b[0].


查看完整回答
反對 回復 2023-10-13
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

您可以將 a 和 b 變量聲明為全局變量


int a,b;


private static void shuffle(Card [] cardArray)

{

    for (int i = 1; i <= 20; s++)

    { 

       randomint();

       Card temp = cardArray[a];

       cardArray[a] = a;

       cardArray[b] = b;

    }

}


private static void randomInt()

{   

    a = (Math.random() * 12);

    b = (Math.random() * 12);

}




查看完整回答
反對 回復 2023-10-13
  • 5 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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