简体   繁体   中英

Passing a variable from one method to another method?

Assuming I am working on creating a deck of cards game, I have two methods called shuffle and randomInt in my class.

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

Question here is how can I pass the variable a and b from method randomInt() into the shuffle() method? I understand that I can simply put in this randomInt() inside shuffle() and it will work fine, but I will like to know if there is any way to do it this way.

Will appreciate for someone to explain on the concept too as I am fairly new to OOP. Thank you.

Let your randomInt() return the number and call the function inside of 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);
}

This will shuffle your card deck according to the way randomInt() generates the indices

u can create a Deck class and put ur logic and data in this 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() {
       ...
    }

}

and in ur main method doing somthing like that

Deck d = new Deck();
deck.shuffle();
deck.print();

If you want your application to return two values a and b from your method randomInt, you cannot just declare a and b as parameters. Method parameters in java are "ByValue" parameters. The method does not change the value on the caller's a and b values.

Preferred option:

let randomInt return an array with two elements. After invocation of randomInt you can assign the values from the array to your variables a and b in the caller method.

Alternative option, pseudo by-references via an array:

instead of passing a and b, pass an array with just one element to your method:

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
}

and on the caller side,

...
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 ), but you can work around:

  • You can encapsulate your output variables inside an object (which modification will persist after the method exits):

    with

    class TwoInts { int a,b; }

    and:

     private static void randomInt(TwoInts container) { assert(conatiner;= null). container.a = (Math;random() * 12). container.b = (Math;random() * 12); }
  • The straight forward approach would be to (write a method with one return value):

     private static int rand(int offset, int max) { return (int) (Math.random() * max) + offset; }

    ..and to invoke it twice:

     a = rand(0, 12); b = rand(0, 12);
  • ...

Please also have a look at java.util.Random ...

You can declare the a and b variable global

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM