0

这些类在编译时的最终工作要求输入,并且应该在对话框中显示一个字符串数组。

一级

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

    }
}

非常感谢!

4

2 回答 2

3

该问题与未显示的数组(或更好的 ArrayList)无关。问题是 List 一开始就没有有用的信息,因此实际上没有什么可显示的。

代码只是在做你写的事情。你不会在你的牌组中添加任何包含任何有意义信息的牌。查看您在甲板上添加任何内容的位置List<Card>

Card card1 = new Card(); 
cards.add(card1);

您每次都添加一个默认 Card 对象,但没有添加任何有意义的信息。所有有意义的信息都被丢弃了。由于这是家庭作业,我会要求您重新考虑如何更好地做到这一点。

于 2012-08-01T02:23:56.067 回答
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
}
于 2012-08-01T02:55:14.663 回答