gpt4 book ai didi

java - 是什么让我出现这个错误?

转载 作者:行者123 更新时间:2023-12-01 18:22:29 25 4
gpt4 key购买 nike

这是我学校项目中的扑克游戏。我有一些我无法弄清楚的异常(exception)。

一开始,我定义了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 类中,我不断从 Deck 构造函数中收到异常错误。

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 循环的实例卡(这意味着它们 i<3,j<13),为什么我仍然收到异常错误?

unreported exception MyPlayingCardException; must be caught or declared to be thrown
Card card = new Card(j,i);

最佳答案

在 java 中,您必须处理代码可能抛出的异常。此编译器错误表明您的 Card 构造函数可以抛出 MyPlayingCardException 类型的异常,但您的 Deck 构造函数没有执行任何操作来处理它。这里有两种方法。您可以将 Decks 构造函数更改为读取

public Decks() throws MyPlayingCardException
{
//constructor code here
}

或者你可以像这样处理异常

for (int j=0; j<= 13; j++)  {

try
{
Card card = new Card(j,i);
originalDecks.add(card);
}
catch(MyPlayingCardException ex){
//some code to handle MyPlayingCardException here
}
}
}

关于java - 是什么让我出现这个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27305779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com