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

假设我正在创建一副纸牌游戏,我的类中有两种称为 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 还很陌生。谢谢。


ibeautiful
浏览 142回答 5
5回答

九州编程

让您randomInt()返回数字并调用 中的函数shuffle()。private static void shuffle(Card[] cardArray) {&nbsp; &nbsp; for (int i = 1; i <= 20; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int a = randomInt();&nbsp; &nbsp; &nbsp; &nbsp; int b = randomInt();&nbsp; &nbsp; &nbsp; &nbsp; Card temp = cardArray[a];&nbsp; &nbsp; &nbsp; &nbsp; cardArray[a] = cardArray[b];&nbsp; &nbsp; &nbsp; &nbsp; cardArray[b] = temp;&nbsp; &nbsp; }}private static int randomInt() {&nbsp; &nbsp;&nbsp; &nbsp; return (int)(Math.random() * 12);}这将根据randomInt()生成索引的方式洗牌你的牌组

守着一只汪

你可以创建一个 Deck 类并将你的逻辑和数据放在这个类中public class Deck {&nbsp; &nbsp; private Card[] deck = new Card[52];&nbsp; &nbsp; public Deck(){&nbsp; &nbsp; &nbsp; &nbsp; initDeck();&nbsp; &nbsp; }&nbsp; &nbsp; public void shuffle() {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= 20; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int a = (int)(Math.random() * 12);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int b = (int)(Math.random() * (52 - 12));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(a,b);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void swap(int a,int b){&nbsp; &nbsp; &nbsp; &nbsp; Card temp = deck[a];&nbsp; &nbsp; &nbsp; &nbsp; deck[a] = deck[b];&nbsp; &nbsp; &nbsp; &nbsp; deck[b] = temp;&nbsp; &nbsp; }&nbsp; &nbsp; private void print() {&nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; }}在你的主要方法中做类似的事情Deck d = new Deck();deck.shuffle();deck.print();

慕码人2483693

在java中,该方法无法按您的预期工作(https://www.google.com/?q=java+call+by+value ... https://stackoverflow.com/a/40523/592355),但你可以解决:您可以将输出变量封装在一个对象内(该修改将在方法退出后持续存在):和class TwoInts {&nbsp; &nbsp;int a,b;}和:private static void randomInt(TwoInts container) {&nbsp; assert(conatiner != null);&nbsp; &nbsp;&nbsp; container.a = (Math.random() * 12);&nbsp; container.b = (Math.random() * 12);}最直接的方法是(编写一个具有一个返回值的方法):&nbsp;private static int rand(int offset, int max) {&nbsp; &nbsp; return (int) (Math.random() * max) + offset;&nbsp;}..并调用它两次:&nbsp;a = rand(0, 12);&nbsp;b = rand(0, 12);...还请您看一下java.util.Random...

一只名叫tom的猫

如果您希望应用程序从 randomInt 方法返回两个值 a 和 b,则不能仅将 a 和 b 声明为参数。java中的方法参数是“ByValue”参数。该方法不会更改调用者的 a 和 b 值。首选选项:让 randomInt 返回一个包含两个元素的数组。调用 randomInt 后,您可以将数组中的值分配给调用方方法中的变量 a 和 b。替代选项,通过数组进行伪引用:不要传递 a 和 b,而是将一个只有一个元素的数组传递给您的方法:private static void randomInt(int[] a, int[] b){&nbsp;&nbsp; //assuming a[] and b[] both are an array with just one element&nbsp; //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].

大话西游666

您可以将 a 和 b 变量声明为全局变量int a,b;private static void shuffle(Card [] cardArray){&nbsp; &nbsp; for (int i = 1; i <= 20; s++)&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;randomint();&nbsp; &nbsp; &nbsp; &nbsp;Card temp = cardArray[a];&nbsp; &nbsp; &nbsp; &nbsp;cardArray[a] = a;&nbsp; &nbsp; &nbsp; &nbsp;cardArray[b] = b;&nbsp; &nbsp; }}private static void randomInt(){&nbsp; &nbsp;&nbsp; &nbsp; a = (Math.random() * 12);&nbsp; &nbsp; b = (Math.random() * 12);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java