使用 ArrayList 创建一副纸牌
Creating a Deck of Cards using an ArrayList
我知道以前很多人都问过这个问题(来这里之前我环顾了很多地方),但我似乎无法弄清楚如何修复我的代码。我的任务是制作一个静态 makeDeck 方法,该方法 returns 一个包含标准扑克牌的 52 张牌的 ArrayList。我是 ArrayLists 的新手,所以我不完全确定我是否正确初始化了我的 ArrayList makeDeck。这是我目前所拥有的:
public class Card
{
private String mySuit;
private int myValue;
public Card( String suit, int value )
{
mySuit = suit;
myValue = value;
}
public String name()
{
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
return cardNames[ myValue - 2 ] + " of " + mySuit;
}
}
public class MainClass
{
public static void displayCards( ArrayList<Card> a )
{
int i;
System.out.println( "Size is " + a.size() );
for ( i = 0 ; i < a.size() ; i++ )
{
Card c = a.get( i );
System.out.println( "#" + (i+1) + ": " + c.name() );
}
}
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=cardNames.length(); a++)
{
for (int b=0; b<= suits.length(); b++)
{
cards.add(new Card(cardNames[a], suits[b])); //I've noticed many people use this to add the card names and suits to the cards ArrayList, and it gives me an error of not being able to turn suits[b] into an integer, so I'm confused as to how it worked for others
}
}
return cards;
}
public static void main( String[] args )
{
System.out.println(makeDeck());
}
如果有人能帮我弄清楚我做错了什么,我将不胜感激。谢谢!
您需要制作 makeDeck()
return cards
。同样在您的主要打印语句中,您遇到调用 makeDeck()
而不是 cards()
关于你的错误string cannot be converted to int
你需要做以下两件事之一:
1) 更改 Card class 构造函数以接受两个字符串:
Public Card(string value, string suit)
并将int myValue
更改为string myValue
2) 将您的 cardName 数组更改为 int
而不是 string
。您也可以一起忽略 cardNames
并将插入更改为以下内容:
cards.add(new Card(suits[b], a+1));
添加 return
语句,该语句将 return 您的数组列表。
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=12; a++)
{
for (int b=0; b<=3; b++)
{
cards.add(new Card(cardNames[a], suits[b])); //I've noticed many people use this to add the card names and suits to the cards ArrayList, and it gives me an error of not being able to turn suits[b] into an integer, so I'm confused as to how it worked for others
}
}
return cards;
}
从您的主要方法调用 makeDeck()
。
public static void main( String[] args ){
ArrayList<Card> cards = makeDeck();
for(Card c : cards){
System.out.println("You can print card member variables here");
}
}
您正在将两个字符串传递给 Card
构造函数。在您编辑掉 Card
class 的代码之前,我看到您的构造函数需要一个 String 和一个 int。这就是构造函数调用无法编译的原因。
此外,您忘记在 makeDeck()
方法中 return 一个值,并且您的 cards
列表是该方法的本地列表,因此您无法从main 方法,而且你没有 cards()
方法。
你的主要方法应该是:
public static void main( String[] args )
{
System.out.println(makeDeck());
}
您的 makeDeck()
应该在最后一行有一个 return cards;
语句。
并且您应该更改 Card
class 的构造函数或更改传递给它的 argumnets。
使用卡片的当前实现 class,您应该将卡片创建循环更改为:
for (int a=0; a < suits.length; a++)
{
for (int b=0; b< 13 ; b++)
{
cards.add(new Card(suits[a],b));
}
}
尽管如此,您可能必须更改 Card
的 name()
方法。
你似乎忘记了 return
在你的方法的最后,
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=12; a++)
{
for (int b=0; b<=3; b++)
{
cards.add(new Card(cardNames[a], b)); // <-- if the second Card
// parameter whould be an int.
}
}
return cards;
}
你应该打电话给 makeDeck()
而不是 cards()
System.out.println(makeDeck());
我知道以前很多人都问过这个问题(来这里之前我环顾了很多地方),但我似乎无法弄清楚如何修复我的代码。我的任务是制作一个静态 makeDeck 方法,该方法 returns 一个包含标准扑克牌的 52 张牌的 ArrayList。我是 ArrayLists 的新手,所以我不完全确定我是否正确初始化了我的 ArrayList makeDeck。这是我目前所拥有的:
public class Card
{
private String mySuit;
private int myValue;
public Card( String suit, int value )
{
mySuit = suit;
myValue = value;
}
public String name()
{
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
return cardNames[ myValue - 2 ] + " of " + mySuit;
}
}
public class MainClass
{
public static void displayCards( ArrayList<Card> a )
{
int i;
System.out.println( "Size is " + a.size() );
for ( i = 0 ; i < a.size() ; i++ )
{
Card c = a.get( i );
System.out.println( "#" + (i+1) + ": " + c.name() );
}
}
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=cardNames.length(); a++)
{
for (int b=0; b<= suits.length(); b++)
{
cards.add(new Card(cardNames[a], suits[b])); //I've noticed many people use this to add the card names and suits to the cards ArrayList, and it gives me an error of not being able to turn suits[b] into an integer, so I'm confused as to how it worked for others
}
}
return cards;
}
public static void main( String[] args )
{
System.out.println(makeDeck());
}
如果有人能帮我弄清楚我做错了什么,我将不胜感激。谢谢!
您需要制作 makeDeck()
return cards
。同样在您的主要打印语句中,您遇到调用 makeDeck()
而不是 cards()
关于你的错误string cannot be converted to int
你需要做以下两件事之一:
1) 更改 Card class 构造函数以接受两个字符串:
Public Card(string value, string suit)
并将int myValue
更改为string myValue
2) 将您的 cardName 数组更改为 int
而不是 string
。您也可以一起忽略 cardNames
并将插入更改为以下内容:
cards.add(new Card(suits[b], a+1));
添加 return
语句,该语句将 return 您的数组列表。
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=12; a++)
{
for (int b=0; b<=3; b++)
{
cards.add(new Card(cardNames[a], suits[b])); //I've noticed many people use this to add the card names and suits to the cards ArrayList, and it gives me an error of not being able to turn suits[b] into an integer, so I'm confused as to how it worked for others
}
}
return cards;
}
从您的主要方法调用 makeDeck()
。
public static void main( String[] args ){
ArrayList<Card> cards = makeDeck();
for(Card c : cards){
System.out.println("You can print card member variables here");
}
}
您正在将两个字符串传递给 Card
构造函数。在您编辑掉 Card
class 的代码之前,我看到您的构造函数需要一个 String 和一个 int。这就是构造函数调用无法编译的原因。
此外,您忘记在 makeDeck()
方法中 return 一个值,并且您的 cards
列表是该方法的本地列表,因此您无法从main 方法,而且你没有 cards()
方法。
你的主要方法应该是:
public static void main( String[] args )
{
System.out.println(makeDeck());
}
您的 makeDeck()
应该在最后一行有一个 return cards;
语句。
并且您应该更改 Card
class 的构造函数或更改传递给它的 argumnets。
使用卡片的当前实现 class,您应该将卡片创建循环更改为:
for (int a=0; a < suits.length; a++)
{
for (int b=0; b< 13 ; b++)
{
cards.add(new Card(suits[a],b));
}
}
尽管如此,您可能必须更改 Card
的 name()
方法。
你似乎忘记了 return
在你的方法的最后,
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=12; a++)
{
for (int b=0; b<=3; b++)
{
cards.add(new Card(cardNames[a], b)); // <-- if the second Card
// parameter whould be an int.
}
}
return cards;
}
你应该打电话给 makeDeck()
而不是 cards()
System.out.println(makeDeck());