这些类在编译时要求输入的最终工作,并应在对话框中显示字符串数组。
第一类
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;
}
}
二等。我感谢trashgod这个。但是,这里是问题所在。它不会在对话框中显示字符串数组。它什么也没显示。
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();
}
}
非常感谢!
答案 0 :(得分:3)
该问题与未显示的数组(或更好的ArrayList)无关。问题是List没有开头的有用信息,所以没有什么可以显示。
代码正在做你写的。您没有将任何有意义的信息卡添加到您的套牌中。看看你在Deck的List<Card>
:
Card card1 = new Card();
cards.add(card1);
每次添加默认的Card对象时都没有添加任何有意义的信息。所有有意义的信息都被丢弃了。由于这是家庭作业,我会请你重新思考如何做得更好。
答案 1 :(得分:1)
您的代码存在的问题是,卡存储信息(名称)尚未使用并且被丢弃并且作为新卡(没有数据)您正在创建并传递给list
card.setCardName (card.getSuitName() + "-" + card.getRankName()); //hold data
Card card1 = new Card(); // holds nothing (useless)
cards.add(card1); // will have card with no data
public void displayAll() {
JOptionPane.showMessageDialog(null, new JScrollPane(new JList(cards.toArray())));
// has nothing to display
}