我知道以前很多人都问过这个问题(在来这里之前我环顾了一下),但我似乎无法弄清楚如何修复我的代码。 我的任务是创建一个静态的makeDeck方法,该方法返回一个ArrayList,其中包含一副标准纸牌的52张牌。 我对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());
}
如果有人能帮我弄清楚我做错了什么,我将不胜感激。 谢谢!
你正在将两个字符串传递给你的Card
构造函数。在您编辑 Card
类的代码之前,我看到您的构造函数需要一个字符串和一个 int。这就是构造函数调用不编译的原因。
此外,您忘记在 makeDeck()
方法中返回一个值,并且您的cards
列表是该方法的本地列表,因此您无法从 main 方法访问它,并且您没有 cards()
方法。
您的主要方法应该是:
public static void main( String[] args )
{
System.out.println(makeDeck());
}
您的makeDeck()
的最后一行应有一个return cards;
语句。
你应该要么改变你的Card
类的构造函数,要么改变你传递给它的argumnets。
使用卡类的当前实现,您可能应该将卡创建循环更改为:
for (int a=0; a < suits.length; a++)
{
for (int b=0; b< 13 ; b++)
{
cards.add(new Card(suits[a],b));
}
}
不过,您可能需要更改name()
Card
方法。
返回makeDeck()
cards
。 同样在您的主要印刷声明中,您见面是打电话给makeDeck()
而不是cards()
关于你的错误string cannot be converted to int
你需要做以下两件事之一:
1) 将 Card 类构造函数更改为包含两个字符串:
Public Card(string value, string suit)
并将int myValue
更改为string myValue
2) 将您的 cardName 数组更改为 int
而不是 string
。您也可以一起忽略cardNames
并将插入的内容更改为以下内容:
cards.add(new Card(suits[b], a+1));
似乎你忘了在方法结束时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());
添加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");
}
}