Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在游戏桥上使用StringBuilder设置String toString()格式_Java - Fatal编程技术网

Java 在游戏桥上使用StringBuilder设置String toString()格式

Java 在游戏桥上使用StringBuilder设置String toString()格式,java,Java,我需要一些帮助格式化这个字符串方法 现在,我的打印编码真的是一团糟,我有它,所以我的代码几乎是以正确的格式打印,除了13张卡没有打印出来 我混乱的代码如下 /** * Will print out the hand information in a neat format using a * StringBuilder will print 4 cards containing rank,suit,points each line * in order f

我需要一些帮助格式化这个字符串方法

现在,我的打印编码真的是一团糟,我有它,所以我的代码几乎是以正确的格式打印,除了13张卡没有打印出来

我混乱的代码如下

    /**
     * Will print out the hand information in a neat format using a 
     * StringBuilder will print 4 cards containing rank,suit,points each line
     * in order from Clubs,Diamonds,Hearts,Spades if no cards of the suit it 
     * will print a blank line
     */
    public String toString()
    {
        StringBuilder hand = new StringBuilder();
// 
//         for (int i = 0; i < cards.length; i++) {
//             char suit = cards[i].getSuit();
//         }
            // 
            //             for (int j = 0; j < 5; j++) {
            //                 if (suit == 'C')
            //                     hand.append(cards[j] + "  ");
            //                 else if (suit == 'D')
            //                     hand.append(cards[j] + "  ");
            //                 else if (suit == 'H')
            //                     hand.append(cards[j] + "  ");
            //                 else if (suit == 'S')
            //                     hand.append(cards[j]);
            //                 else if (j == 4) 
            //                     hand.append("\n");
            //             }
            for (int j = 0; j < 5; j++) {
                if (cards[j].getSuit() == 'C')
                    hand.append(cards[j] + "  ");
                else if (j == 4) 
                    hand.append("\n");
            }

            for (int j = 0; j < 5; j++) {
                if (cards[j].getSuit() == 'D')
                    hand.append(cards[j] + "  ");
                else if (j == 4)
                    hand.append("\n");
            }

            for (int j = 0; j < 5; j++) {
                if (cards[j].getSuit() == 'H')
                    hand.append(cards[j] + "  ");
                else if (j == 4)
                    hand.append("\n");
            }

            for (int j = 0; j < 5; j++) {
                if (cards[j].getSuit() == 'S')
                    hand.append(cards[j]);
                else if (j == 4)
                    hand.append("\n");
            }

            return hand.toString();
        }
此外,我需要有它,以便我的结束代码打印出这种格式(示例)


总共只有5张牌出来,因为你在相同的范围[0..4]内循环每套衣服。嗯,打印的数量不可能超过5个,所以我认为应该是这样的:

for (Card card : cards) {
   if(card.getSuit() == 'C')
       hand.append(card + "  ");
}
hand.append("\n"); //I just moved this line out of loop
然后对所有西装都这样做:

for (Char suit : new Char[]{'C', 'D', 'H', 'S'}) {
  for (Card card : cards) {
     if(card.getSuit() == suit)
        hand.append(card + "  ");
  }
  hand.append("\n");
}

不太清楚所需的结果是什么最下面的代码就是我需要的结果。不要使用
\t
,使用
String\format
,这将允许设置每列应有的字符数。看看
for (Card card : cards) {
   if(card.getSuit() == 'C')
       hand.append(card + "  ");
}
hand.append("\n"); //I just moved this line out of loop
for (Char suit : new Char[]{'C', 'D', 'H', 'S'}) {
  for (Card card : cards) {
     if(card.getSuit() == suit)
        hand.append(card + "  ");
  }
  hand.append("\n");
}