如何减少代码中的重复性?

时间:2014-11-15 18:32:08

标签: java applet japplet

查看以下程序:

public class HouseOfCards 
{
    public static void main(String[] args)
    {
        for (int cards = 1; cards <= 4; cards++)
        {
           if (cards == 1) 
           {
               System.out.println("Ace of Clubs");
                for (int singles = 2; singles <= 9; singles++)
                { 
                   System.out.println(singles + " of Clubs");
                }//end of for loop()
               System.out.println("Jack of Clubs");
               System.out.println("Queen of Clubs");
               System.out.println("King of Clubs");
               System.out.println("Ace of Clubs");
          }//end of if() 
                            ......
             //More else if() blocks for each suit
                            ......
        }//end of for loop()
     }//end of method main() 
   }//end of class HouseOfCards

在上面的代码中,我想打印第一组卡片,即俱乐部,然后以“新的套牌顺序”格式对其余套装执行相同的操作。

分会 - &gt;黑桃 - &gt;心 - &gt;钻石

我看到第一个if()块,即(cards == 1),有点重复。我不想做4块积木做整个套牌。

我的问题如下, 1.我将如何以这种方式减少代码? 2.有可能吗? 要么 3.每件套装最好做4组if()块吗?

提前感谢您的帮助!

8 个答案:

答案 0 :(得分:2)

private String[] cards = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private String[] colors = {"Clubs", "Spades", "Hearts", "Diamonds"};

然后使用for循环遍历两个数组。

for (int iColor = 0; iColor < colors.length; iColor++) {
    for (int iCard = 0; iCard < cards.length; iCard++) {
        System.out.printf("%s of %s%n", cards[iCard], colors[iColor];
    }
}

答案 1 :(得分:1)

如果您遇到的唯一问题是打印套装,那么我会创建一个数组:

String[] suits = {"Clubs", "Spades", "Hearts", "Diamonds"};

ArrayList<String>()

ArrayList<String> suits = new ArrayList<String>();
suits.add("Clubs");
suits.add("Spades");
suits.add("Hearts");
suits.add("Diamonds");

你可以简单地迭代它:

for(String suit : suits)

答案 2 :(得分:0)

制作它的数组:
String[] arr = new String[]{"clubs","spades","hearts","diamonds"}
然后使用循环:
for(int i=0;i<arr.length;i++){} 例如:

public class HouseOfCards 
{
    private static final String[] arr = new String[]{"Clubs","Spades","Hearts","Diamonds"};
    public static void main(String[] args)
    {
            for(int i=0;i<arr.length;i++)
            {
            System.out.println("Ace of "+arr[i]);
                for (int singles = 2; singles <= 9; singles++)
                { 
                   System.out.println(singles + " of "+arr[i]);
                }//end of for loop()
               System.out.println("Jack of "+arr[i]);
               System.out.println("Queen of "+arr[i]);
               System.out.println("King of "+arr[i]);
               System.out.println("Ace of "+arr[i]);
           }//end of if()
     }//end of method main() 
   }//end of class HouseOfCards

答案 3 :(得分:0)

创建方法printSuit(String suitName)并在每个if语句中使用它。

您还可以创建Enum套装并迭代其值。

答案 4 :(得分:0)

出于算法理解的目的:

如果您的For包含X值且每个值都在if内,则只需移除forif

for (int cards = 1; cards <= 4; cards++){
       if (cards == 1) System.out.println("A");
       if (cards == 2) System.out.println("B");
       if (cards == 3) System.out.println("C");
       if (cards == 4) System.out.println("D");
}

与:

完全相同
System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("D");

答案 5 :(得分:0)

使用enum最适合您的额外功能 这是我的方法,使你的代码变得简单。

public static void main(String...args){
    for(Card card : Card.values()){
        showCards(card);
    }
}
static void showCards(Card card){
    for(CardVal cv : CardVal.values()){
        System.out.println(cv + " of "+card);
    }
}
static enum Card {
    Club, 
    Spades,
    Hearts,
    Diamond
}
static enum CardVal {
    Ace,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King
}

答案 6 :(得分:0)

更面向对象的解决方案是创建一个Card类,并将枚举类型的套装和值提升为:

public class Card {

    public enum Suit {
        HEARTS,
        CLUBS,
        SPADES,
        DIAMONDS;
    }

    public enum FaceValue {
    ACE,
    KING,
    QUEEN,
    JACK,
    TEN,
    NINE,
    EIGHT,
    SEVEN,
    SIX,
    FIVE,
    FOUR,
    THREE,
    TWO,
    ONE
    }

    private Suit suit;
    private FaceValue value;

    public Card(Suit suit, FaceValue value) {
        this.suit = suit;
        this.value = value;
    }

    @Override
    public String toString() {
        return value.toString() + " of " + suit.toString();
    }

}

然后,您可以将打印代码缩减为两个嵌套循环:

public static void main(String[] args) {

    for(Suit s : Suit.values()) {
        for (FaceValue v : FaceValue.values()) {
            System.out.println(new Card(s,v));
        }
    }
}

答案 7 :(得分:0)

由于java是面向对象的语言,尝试思考对象

首先使用颜色和等级创建枚举;

    enum Colour{
        Clubs,Diamonds,Hearts,Spades;
    }

    enum Rank{
         Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King,Ace
    }

定义您的卡

class Card{
    @Override
    public String toString() {
        return rank + " of "+colour;
    }
    public Card(Colour colour, Rank rank) {
        super();
        this.colour = colour;
        this.rank = rank;
    }
    private final Colour colour;
    private final Rank rank;

}

如果你的卡会实现类似的界面会很好,那么如果你尝试创建任何纸牌游戏它可能是有用的

最后你需要的是甲板

class Deck{
    List<Card> cards = new ArrayList<Card>();       
    public Deck(){
        for (Colour colour : Colour.values()){
            for (Rank rank : Rank.values()){
                cards.add(new Card(colour, rank));
            }   
        }
    }
}
当实例化时,

牌创建所有牌,它可以用来操纵牌