이것은 내 학교 프로젝트의 포커 게임입니다. 내가 알아낼 수없는 예외가있다.이 오류는 무엇입니까?
처음에는 Card 클래스를 정의했습니다.
class Card {
/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
/* Data field of a card: rank and suit */
private int cardRank; /* values: 1-13 (see Rank[] above) */
private int cardSuit; /* values: 0-3 (see Suit[] above) */
/* Constructor to create a card */
/* throw MyPlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws MyPlayingCardException {
if ((rank < 1) || (rank > 13))
throw new MyPlayingCardException("Invalid rank:"+rank);
else
cardRank = rank;
if ((suit < 0) || (suit > 3))
throw new MyPlayingCardException("Invalid suit:"+suit);
else
cardSuit = suit;
}
/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }
/* Few quick tests here */
public static void main(String args[])
{
try {
Card c1 = new Card(1,3); // A Spades
System.out.println(c1);
c1 = new Card(10,0); // 10 Clubs
System.out.println(c1);
c1 = new Card(10,5); // generate exception here
}
catch (MyPlayingCardException e)
{
System.out.println("MyPlayingCardException: "+e.getMessage());
}
}
}
나는이 클래스의 예외를 만들었습니다. 그런 다음 Decks 클래스에서 예외 생성 오류가 계속 발생합니다.
class Decks {
/* this is used to keep track of original n*52 cards */
private List<Card> originalDecks;
/* this starts with n*52 cards deck from original deck */
/* it is used to keep track of remaining cards to deal */
/* see reset(): it resets dealDecks to a full deck */
private List<Card> dealDecks;
/* number of decks in this object */
private int numberDecks;
/**
* Constructor: Creates default one deck of 52 playing cards in originalDecks and
* copy them to dealDecks.
* initialize numberDecks=n
* Note: You need to catch MyPlayingCardException from Card constructor
* Use ArrayList for both originalDecks & dealDecks
*/
public Decks()
{
// implement this method!
ArrayList<Card> originalDecks = new ArrayList<Card>(52);
ArrayList<Card> dealDecks = new ArrayList<Card>(52);
for (int i=0; i<=3; i++) {
for (int j=0; j<= 13; j++) {
Card card = new Card(j,i);
originalDecks.add(card);
}
}
dealDecks.addAll(originalDecks);
}
왜 그렇습니까? 인스턴스 카드를 for 루프에서 사용하면 (즉, < 3, j < 13), 예외 오류가 발생하는 이유는 무엇입니까?
unreported exception MyPlayingCardException; must be caught or declared to be thrown
Card card = new Card(j,i);
Paul, Card()에 예외를 던지면 메모리로 이동하지 않아 다음 클래스에서 다시 던져서 잡아야한다는 말입니까? –
RuntimeException이 아닌 한 어디서나 호출 경로를 따라 잡아서 던져야합니다. – PaulProgrammer
나는 다음과 같이 try ... catch에 Card Card = new Card (j, i)를 사용한다. try {Card card = new Card (j, i); } catchcatch (MyPlayingCardException e) {System.out.println ("MyPlayingCardException :"+ e.getMessage());}하지만 그 뒤에있는 행에 오류가 발생합니다. 여기서 내가 실수 한거야? –