2

我在这里有三个类,它们要求输入并且应该在对话框中显示一个字符串数组。

第一类:

public class Card {

  public int rank;
  public int suit;
  public String rankName;
  public String suitName;
  public String cardName;

  public Card () {
    rank = 0;
    suit = 0;
    rankName = "";
    suitName = "";
    cardName = "";
  }

  public int getRank() {
    return rank;
  } 
  public void setRank(int rank) {
    this.rank = rank;
  }
  public int getSuit() {
    return suit;
  }
  public void setSuit(int suit) {
    this.suit = suit;
  }
  public String getRankName() {
    return rankName;
  }
  public void setRankName(String rankName) {
    this.rankName = rankName;
  }
  public String getSuitName() {
    return suitName;
  }
  public void setSuitName(String suitName) {
    this.suitName = suitName;
  }
  public String getCardName() {
    return cardName;
  }
  public void setCardName(String cardName) {
    this.cardName = cardName;
  }
}

第 2 类 - 这是我应该输出存储在数组中的字符串的类。我认为我在做仿制药部分的方向是正确的,但我不喜欢那样做。请指导我打印所有这些。

import javax.swing.JOptionPane;

public class Deck {

    public void assignIntegerValues(Card card) {
        //Card card = new Card();
        String[] suitNameArray = {"Clubs", "Spades", "Hearts", "Diamonds"};
        String[] rankNameArray = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
        List<Card> cardName = new ArrayList();

        card.setSuitName  (suitNameArray[card.getSuit()-1]);
        card.setRankName (rankNameArray[card.getRank()-1]);
        card.setCardName (card.getSuitName() + "-" + card.getRankName());
        JOptionPane.showMessageDialog(null, card.cardName);
    }

    public void displayAll (Card card) {
        for (int i=0; i<card.cardName.length(); i++) {
            String print = JOptionPane.showMessageDialog(null, card.cardName[i]);
        }
    }
}

第 3 类:

import javax.swing.JOptionPane;

public class DisplayDeck {

    public static void main(String[] args) {

        Card card = new Card();
        Deck deck = new Deck();
        int answer;
        String again;

        do {
            String suitString = JOptionPane.showInputDialog("Suit: ");
            card.setSuit (Integer.parseInt(suitString));

            String rankString = JOptionPane.showInputDialog("Rank: ");
            card.setRank (Integer.parseInt(rankString)); 

            deck.assignIntegerValues(card);
            String answerString = JOptionPane.showInputDialog("Try again? (1/0) ");
            answer = Integer.parseInt(answerString);
        } while (answer == 1);

        deck.displayAll(card);

    }
}

我正在尝试显示用户输入的 cardNames。

4

2 回答 2

3
public void displayAll (Card[] cards) {
    String output = "";
    for (int i=0; i<cards.length(); i++) {
        output += cards[i].getCardName() + "\n";
    }
    JOptionPane.showMessageDialog(null, output);
}

您需要将数组传递给 displayAll 否则您只有一张卡。

然后在实际创建 JOptionPane 之前构建输出字符串。

于 2012-07-31T16:19:01.603 回答
1

另一种方法取决于Deck有一个添加每个新的集合Card

class Deck {
    private List<Card> cards = new ArrayList<Card>();
    ...
}

Card覆盖toString(),

@Override
public String toString() {
    return cardName;
}

然后displayAll()变得非常简单:

public void displayAll() {
    JOptionPane.showMessageDialog(null,
        new JScrollPane(new JList(cards.toArray())));
}

图片

于 2012-07-31T17:31:14.827 回答