我知道很多人之前已经问过这个问题(我来之前看了很多),但我似乎无法弄清楚如何修复我的代码。我的任务是创建一个静态makeDeck方法,该方法返回一个包含标准牌组的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());
}
如果有人能帮助我弄清楚我做错了什么,我将非常感激。谢谢!
答案 0 :(得分:2)
您正在将两个字符串传递给Card
构造函数。在编辑Card
类的代码之前,我看到你的构造函数需要一个String和一个int。这就是构造函数调用无法编译的原因。
此外,您忘记在makeDeck()
方法中返回值,并且您的cards
列表是该方法的本地列表,因此您无法通过主方法访问该列表,并且你还没有cards()
方法。
您的主要方法应该是:
public static void main( String[] args )
{
System.out.println(makeDeck());
}
您的makeDeck()
最后一行应该有一个return cards;
声明。
您应该更改Card
类的构造函数或更改传递给它的argumnets。
使用Card类的当前实现,您可能应该将卡创建循环更改为:
for (int a=0; a < suits.length; a++)
{
for (int b=0; b< 13 ; b++)
{
cards.add(new Card(suits[a],b));
}
}
您可能不得不更改name()
的{{1}}方法。
答案 1 :(得分:1)
您需要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));
答案 2 :(得分:0)
添加将返回数组列表的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");
}
}
答案 3 :(得分:0)
您的方法结束时似乎忘了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());