Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/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 如何减少代码中的重复性?_Java_Applet_Japplet - Fatal编程技术网

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

Java 如何减少代码中的重复性?,java,applet,japplet,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");

请看以下程序:

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
public-classhouseofcards
{
公共静态void main(字符串[]args)
{
对于(整型卡=1;卡心-->钻石)

我看到第一个if()块,(cards==1)有点重复,我不想用4个if块来完成整个组

我向您提出的问题如下:, 1.我将如何以这种方式减少代码? 2.可能吗? 或 3.是否最好为每件西装制作4组if()方块?

提前感谢您的帮助!

制作数组:
String[]arr=新字符串[]{“梅花”、“黑桃”、“红心”、“钻石”}

然后使用循环:

for(int i=0;i创建一个方法
printSuit(String suitName)
并在每个
if
语句中使用它

您还可以创建西装的
Enum
,并迭代其值

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];
    }
}
for(int-iColor=0;iColor
如果您唯一的问题是打印西装,那么我会创建一个数组:

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


为了理解算法,请执行以下操作:

如果您有一个带有X个值的
For
,每个值都在
If
内,那么只需删除
For
If

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");
}

使用
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
}

一个更面向对象的解决方案是创建一个Card类,并将suit和value提升为枚举类型,如下所示:

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));
        }
    }
}

由于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));
            }   
        }
    }
}
类甲板{
列表卡=新的ArrayList();
公共甲板(){
用于(颜色:color.values()){
for(秩:Rank.values()){
卡片。添加(新卡片(颜色、等级));
}   
}
}
}

卡片组当实例化创建所有卡片时,它可以用来操作卡片

谢谢你的回答,我发现这是最好的方法!这似乎是解决我问题的最简单的方法。谢谢你的输入!
public static void main(String[] args) {

    for(Suit s : Suit.values()) {
        for (FaceValue v : FaceValue.values()) {
            System.out.println(new Card(s,v));
        }
    }
}
    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));
            }   
        }
    }
}