繁体   English   中英

使用ArrayList创建卡片组

[英]Creating a Deck of Cards using an ArrayList

我知道以前很多人都问过这个问题(在来到这里之前我环顾了一下),但是我似乎无法弄清楚如何解决我的代码。 我的任务是制作一个静态makeDeck方法,该方法返回一个ArrayList,其中包含标准纸牌中的52张纸牌。 我是ArrayList的新手,所以我不确定我是否正确初始化了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());
    }

如果有人可以帮助我弄清楚我在做什么错,我将不胜感激。 谢谢!

您正在将两个String传递给Card构造函数。 在您编辑Card类的代码之前,我看到您的构造函数需要一个String和一个int。 这就是构造函数调用无法编译的原因。

另外,您忘了在makeDeck()方法中返回值,并且您的cards列表在该方法的本地,因此您无法从主方法访问它,而且您也没有cards()方法。

您的主要方法应为:

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

您的makeDeck()应该有一个return cards; 最后一行中的语句。

您应该更改Card类的构造函数或更改传递给它的argumnet。

使用Card类的当前实现,您可能应该将Card创建循环更改为:

    for (int a=0; a < suits.length; a++)
    {
        for (int b=0; b< 13 ; b++)
         {
           cards.add(new Card(suits[a],b));  
         }
    }

不过,您可能必须更改Cardname()方法。

您需要使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], 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");
 } 

}

看来您忘了在方法结束时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());

暂无
暂无

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

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