Java中的交易卡

时间:2015-12-15 10:33:53

标签: java

我是编程的新手,所以如果我的代码很难理解,请原谅。我被分配编程游戏以向用户发卡并且它是顶级卡。我已经制作了卡片课程。我只需要知道我是否正确地这样做了。它说建立成功,但没有显示任何东西。请帮忙!

package deck;

import java.util.*;

/**
 *
 * @author useradmin
 */

// Write a description of class Deck here.
public class Deck {

    private Card[] theCards;
    private int deal;

    public Deck() {
        theCards = new Card[52];
        deal = 52;
        this.fill();
        //fill();
    }
    public int deal() {
        return deal;

    }

    public Card getCard() {
        Card a = null;
        a = theCards[deal-1];
        deal--;
        return a;
    }

    public String toString()
    {
        String deckString = "New deck shuffled\n";

        for(int i = 1; i <= 1; i++)
        {
            deckString += theCards[i].toString() + "\n";
        }
        return deckString;
    }
    public void shuffleCards() {
        Random random = new Random();
        Card temp;
        int topCard;
        for(int i = 0; i<30; i++){
            topCard = random.nextInt(deal);
        }
    }
    private void fill() {
        int i, j;
        int index = 0;
        for(i = 0; i <4; i++) {
            for(j = 1; j < 14; j++){
                theCards[index] = new Card(i, j);
                index++;
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here





    }

    {
    }
}

1 个答案:

答案 0 :(得分:2)

public static void main(String[] args)方法是程序的入口点。当您运行程序时,这是第一个被调用的方法。你的是空的,所以什么都不会发生。

的解决方案

创建一个新课程,并将其命名为Application

public static void main(String[] args){}课程中剪切Deck方法并将其粘贴到新的Application课程中。

main()内,您需要输入一些代码!我建议创建一个Deck对象,然后使用toString()方法打印套牌内容,以便您可以看到一切正常。

您的新课程应如下所示:

public class Application {

    //Main method (Entry point for program)
    public static void main(String[] args) {

        Deck myDeck = new Deck(); //Create Deck

        System.out.println(myDeck.toString()); //Print contents of Deck
    }
}

确保您已从main()课程中删除了Deck方法。

希望能帮到你。 :)

相关问题