为了账号安全,请及时绑定邮箱和手机立即绑定

将变量从一种方法传递到另一种方法?

将变量从一种方法传递到另一种方法?

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条经验 获得超3个赞

你可以创建一个 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条经验 获得超2个赞

如果您希望应用程序从 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 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信