【问题标题】:Java card game. How to call in mainJava纸牌游戏。如何调用主
【发布时间】:2017-09-15 01:02:17
【问题描述】:

这个程序的目的是创建一副卡片,方法的名称是不言自明的。我想我所有的方法都是正确的,但我需要在 main 中测试它们,我不确定如何。我是对象、数组和直方图的新手。有关如何测试某些数组/直方图方法的任何示例都会有所帮助。只是努力学习和理解。谢谢!

例如,我想测试的方法之一是 printDeck。所以主要是,我输入“printDeck(deck);”但它需要知道从哪里获得甲板,我对如何做到这一点感到困惑。

class Cards {
int suit, rank;


public Cards() {
    this.suit = 0;
    this.rank = 1;
}


public Cards(int suit, int rank) {
    this.suit = suit;
    this.rank = rank;
}

public static int compareCard (Cards c1, Cards c2) {
    if (c1.suit>c2.suit) return 1;
    else if (c1.suit<c2.suit) return-1;
    else {
        if (c1.rank == 1)c1.rank = 14; 
        if (c2.rank == 1)c2.rank = 14;
        if (c1.rank>c2.rank) {
            if (c1.rank == 14)c1.rank = 1;
            return 1;
        } else if (c1.rank<c2.rank) {
            if (c2.rank == 14)c2.rank = 1;
            return -1;
        } else {
            if (c1.rank == 14)c1.rank = 1;
            if (c2.rank == 14)c2.rank = 1;
            return 0;
        }
    }
}


public static void buildDeck () {
    Cards[] deck = new Cards[52];
    int i = 0;
    for (int s = 0; s<4; s++) {
        for (int r = 1; r<14; r++) {
            deck[i] = new Cards(s, r);
            i++;
        }
    }
}


public static int handScore (Cards[] ar) {
    int x = 0;
    for (int i =0; i< ar.length; i++) {
        if (ar[i].rank > 10) x += (ar[i].rank-10);
    }
    int result = 0;
    for (int i =0; i< ar.length; i++) {
        result += ar[i].rank;
    }
    return result-x;
}


public static Cards parseCard(String s) {
    Cards emptyCard = null;
    String[] suits = {" ", "Clubs", "Diamonds", "Hearts", "Spades"};
    String[] ranks = {"n", "Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
    String sr = s.substring(0, s.indexOf(' '));
    String ss = s.substring (s.lastIndexOf(' ')+1, s.length()-1);
    int holdRank = 0;
    for (int i=0; i<ranks.length; i++) {
        if (ranks[i].equals(sr)) holdRank = i;
    }
    if (holdRank == 0) return emptyCard;
    int holdSuit = 0;
    for (int i=0; i<suits.length; i++) {
        if (ranks[i].equals(sr)) holdSuit = i;
    }
    if (holdSuit == 0)return emptyCard;
    Cards result = new Cards (holdSuit-1, holdRank);
    return result;
}

public static int[] suitHist (Cards[] ar) {
    int[] hist = {0, 0, 0, 0};
    for (int i = 0; i<ar.length; i++) {
        hist[ar[i].suit]++;
    }
    return hist;
}


public static boolean isFlush (Cards[] ar) {
    int[] h = suitHist(ar);
    if (h[0] > 4 || h[1] > 4 || h[2] > 4|| h[3] > 4) return true;
    else return false;
}


public static void printCard (Cards c) {
 String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
 String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
 "7", "8", "9", "10", "Jack", "Queen", "King" };
 System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
} 

public static void printDeck (Cards[] deck) {
 for (int i=0; i<deck.length; i++) {
 printCard (deck[i]);
 }
}

public static void main(String[] args) {

}

}

【问题讨论】:

  • 由于都是静态方法,所以调用它们需要从main传递的参数即可。
  • 反对者,请解释
  • 这个问题没有错。 OP 正在寻找与想法相关的信息。
  • 谢谢大家。很抱歉问了这样一个初学者的问题,但我只是在努力学习。对你们中的一些人来说,这可能看起来很愚蠢,但@cricket_007 确实帮助我理解了如何调用对象,我现在可以将它应用到我的其他方法中。由于人们讨厌我的问题不是超级难的事实,我多次伤害了我的感情。我们需要接受我们都处于编码旅程的不同阶段。感谢那些帮助过的人。 :)
  • 随意翻阅 Oracle 网站上的官方 Java 教程以更好地掌握

标签: java arrays object histogram


【解决方案1】:

我认为我所有的方法都是正确的,

不完全是……

你建立套牌的方法实际上应该给你一个套牌。

public static Card[] buildDeck ()  {
   ... 
    return deck;
} 

这应该可以帮助您获得主要的套牌。

Card[] cards = buildDeck();
printDeck(cards);

测试程序的正确方法,虽然通常是单元测试,而不是主要方法

【讨论】:

  • 谢谢。现在,当我在 main 中打印卡片组时,我输入“System.out.println(buildDeck());”并且输出是“[LCards;@15db9742”。我如何让它真正打印一副纸牌?
  • 这段代码是你写的吗?你有printDeck,所以使用它
  • 啊抱歉我很慌张。我的意思是输入“System.out.println(printDeck(deck));”然后问怎么定义变量deck,定义成什么。
  • 无法打印 void 方法。如果您使用的是 IDE,那将是一个错误。请查看更新的答案
  • 非常感谢您的耐心等待!!我想我现在明白了要点。非常有帮助。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2013-04-17
相关资源
最近更新 更多