簡體   English   中英

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

[英]Passing a variable from one method to another method?

假設我正在創建一副紙牌游戲,我的 class 中有兩種方法,稱為 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);
}

這里的問題是如何將變量ab從方法randomInt()傳遞到shuffle()方法? 我知道我可以簡單地將這個randomInt()放入shuffle()中,它會正常工作,但我想知道是否有任何方法可以這樣做。

也會感謝有人解釋這個概念,因為我對 OOP 還很陌生。 謝謝你。

讓您的randomInt()返回數字並在shuffle()中調用 function 。

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()生成索引的方式洗牌

您可以創建一個甲板 class 並將您的邏輯和數據放入此 class

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();

如果您希望您的應用程序從您的方法 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].

In java, the method doesn't work as you supposed to ( https://www.google.com/?q=java+call+by+value ... https://stackoverflow.com/a/40523/592355 ),但您可以解決:

  • 您可以將 output 變量封裝在 object 中(該修改將在方法退出后持續存在):

    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 ...

您可以將 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM