简体   繁体   English

使用来自另一个 class 的参数调用方法

[英]Call Method with parameters from another class

package com.test.game;

import java.util.ArrayList;

import java.util.Collections;
import java.util.Random;

public class Card {
    private static String[] colours = new String[]{"E", "L", "H", "S"};
    private static String[] cardValues = new String[]{"7", "8", "9", "10", "B", "D", "K", "A"};
    private String cardValue;
    private String colour;

    private Card(String cardValue, String colour) {
        this.cardValue = cardValue;
        this.colour = colour;
    }





    public String toString() {
        return cardValue + colour;
    }



    static void CardDeck() {
        ArrayList<Card> cards = new ArrayList<Card>();
        for (int i = 0; i < colours.length; i++) {
            for (int j = 0; j < cardValues.length; j++) {
                cards.add(new Card(cardValues[j], colours[i]));
            }
        }
        System.out.println(cards);


    }
    static void Collections(ArrayList<Card> cards, int seed){
        Collections.shuffle(cards, new Random(seed));
        System.out.println(cards);
    }

    public static void main(String[] args) {
        System.out.println();
    }



}

package com.test.game;

import java.util.ArrayList;
import java.util.Random;

public class Game {
    public static void main(String[] args) {
        Card.CardDeck();
        Card.Collections();

    }
}

So I am working on a card game right now.所以我现在正在做一个纸牌游戏。 The first class creates an array list containing cards with the help of the method CardDeck() this method gets called in the Game class and that works perfectly fine.第一个 class 在方法CardDeck()的帮助下创建了一个包含卡片的数组列表,该方法在游戏 class 中被调用,并且效果很好。 Now in the Method Collections() this array list is supposed to be shuffled.现在在 Method Collections() 中,这个数组列表应该被打乱。 So that the cards are in a random order.这样卡片的顺序是随机的。

Therefore I have 2 questions.因此我有2个问题。 First is the way I am shuffling the cards right?首先是我洗牌的方式对吗? And how can I call this Collectinons() method in another class?以及如何在另一个 class 中调用此Collectinons()方法? Due to the fact that it has parameters it does not work.由于它有参数,所以它不起作用。 I have found some similar questions but they did not really work for me.我发现了一些类似的问题,但它们并没有真正为我工作。 (creating a new instance) (创建一个新实例)

Can someone help?有人可以帮忙吗?

Instead of printing the list in CardDeck (that you should rename to cardDeck ), just return the deck, so that the caller can use it:不要在CardDeck中打印列表(您应该重命名为cardDeck ),只需返回卡片组,以便调用者可以使用它:

static List<Card> cardDeck() {
    ArrayList<Card> cards = new ArrayList<Card>();
    for (int i = 0; i < colours.length; i++) {
        for (int j = 0; j < cardValues.length; j++) {
            cards.add(new Card(cardValues[j], colours[i]));
        }
    }
    return cards;
}

You can then pass it to your method Collections (that you should rename to shuffle ):然后,您可以将其传递给您的方法Collections (您应该重命名为shuffle ):

static void shuffle(List<Card> cards, int seed){
    Collections.shuffle(cards, new Random(seed));
}

And don't print it there: the caller can always print it afterwards.并且不要在那里打印它:调用者总是可以在之后打印它。

Your main method becomes:您的main方法变为:

public static void main(String[] args) {
    List<Card> deck = Card.cardDeck();
    Card.shuffle(deck, whatever);
    System.out.println(deck);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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