这些类在编译时的最终工作要求输入,并且应该在对话框中显示一个字符串数组。
一级
public class Card {
private int rank;
private int suit;
private String rankName;
private String suitName;
private 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;
}
public String toString() {
return cardName;
}
}
2 级。我感谢垃圾神。但是,问题出在哪里。它不会在对话框中显示字符串数组。它什么也不显示。
import java.util.*;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
public class Deck {
public List<Card> cards = new ArrayList<Card>();
public void assignIntegerValues(Card card) {
String[] suitNameArray = {"Clubs", "Spades", "Hearts", "Diamonds"};
String[] rankNameArray = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
card.setSuitName (suitNameArray[card.getSuit()-1]);
card.setRankName (rankNameArray[card.getRank()-1]);
card.setCardName (card.getSuitName() + "-" + card.getRankName());
JOptionPane.showMessageDialog(null, card.getCardName());
Card card1 = new Card();
cards.add(card1);
}
public void displayAll() {
JOptionPane.showMessageDialog(null, new JScrollPane(new JList(cards.toArray())));
}
}
第三类
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class DisplayDeck {
public static List<Card> cards = new ArrayList<Card>();
public static void main(String[] args) {
Card card = new Card();
Deck deck = new Deck();
int answer;
String again;
String[] nameOfCards;
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();
}
}
非常感谢!