使用来自另一个 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();
}
}
所以我现在正在制作纸牌游戏。第一个 class 在方法 CardDeck()
的帮助下创建了一个包含卡片的数组列表,此方法在游戏 class 中被调用并且工作得很好。现在在方法 Collections() 中,这个数组列表应该被洗牌。这样卡片的顺序是随机的。
因此我有两个问题。首先是我洗牌的方式对吗?我如何在另一个 class 中调用此 Collectinons()
方法?由于它有参数,所以它不起作用。我发现了一些类似的问题,但它们并没有真正为我工作。 (创建新实例)
有人可以帮忙吗?
不是打印 CardDeck
中的列表(你应该重命名为 cardDeck
),只是 return 卡片组,以便调用者可以使用它:
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;
}
然后您可以将它传递给您的方法 Collections
(您应该将其重命名为 shuffle
):
static void shuffle(List<Card> cards, int seed){
Collections.shuffle(cards, new Random(seed));
}
并且不要在那里打印它:调用者可以随时打印它。
您的 main
方法变为:
public static void main(String[] args) {
List<Card> deck = Card.cardDeck();
Card.shuffle(deck, whatever);
System.out.println(deck);
}
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();
}
}
所以我现在正在制作纸牌游戏。第一个 class 在方法 CardDeck()
的帮助下创建了一个包含卡片的数组列表,此方法在游戏 class 中被调用并且工作得很好。现在在方法 Collections() 中,这个数组列表应该被洗牌。这样卡片的顺序是随机的。
因此我有两个问题。首先是我洗牌的方式对吗?我如何在另一个 class 中调用此 Collectinons()
方法?由于它有参数,所以它不起作用。我发现了一些类似的问题,但它们并没有真正为我工作。 (创建新实例)
有人可以帮忙吗?
不是打印 CardDeck
中的列表(你应该重命名为 cardDeck
),只是 return 卡片组,以便调用者可以使用它:
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;
}
然后您可以将它传递给您的方法 Collections
(您应该将其重命名为 shuffle
):
static void shuffle(List<Card> cards, int seed){
Collections.shuffle(cards, new Random(seed));
}
并且不要在那里打印它:调用者可以随时打印它。
您的 main
方法变为:
public static void main(String[] args) {
List<Card> deck = Card.cardDeck();
Card.shuffle(deck, whatever);
System.out.println(deck);
}