gpt4 book ai didi

java - 扑克游戏中未处理的异常

转载 作者:行者123 更新时间:2023-11-29 03:26:54 25 4
gpt4 key购买 nike

我有一个多部分程序(2 个 java 文件)应该做两件事:

  1. 制作一副牌(洗牌、发牌、重置)

  2. 创建一个扑克游戏(制作两副纸牌,其他东西...)

由于这个未处理的异常错误,我遇到了一个障碍,我什至不确定它来自哪里。错误如下图所示,下面是源代码。

Unhandled Exception Error

.

扑克牌异常类

class PlayingCardException extends Exception {

/* Constructor to create a PlayingCardException object */
PlayingCardException (){
super ();
}

PlayingCardException ( String reason ){
super ( reason );
}
}

.

甲板类 /** Decks 类代表:n 副扑克牌 * 使用 Card 类构造 n * 52 张扑克牌! * * 不要添加新的数据字段! * 不要修改任何方法 * 您可以添加私有(private)方法 */

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 n decks (52 cards each deck) of playing cards in
* originalDecks and copy them to dealDecks.
* initialize numberDecks=n
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & dealDecks
* @throws PlayingCardException
*/
public Decks(int n) throws PlayingCardException
{
// implement this method!
this.originalDecks = new ArrayList<Card>();
this.dealDecks = new ArrayList<Card>();
int i, j, k;
numberDecks = n;
for (k=0;k<n;k++){
for (i=1;i<14;i++)
{
for(j=0;j<4;j++)
{
Card orcard = new Card(i,j);
originalDecks.add(orcard);
dealDecks.add(orcard);
}
}
}
}
}

最佳答案

这个已检查的异常是在 Decks 类的构造函数中声明的。

public Decks(int n) throws PlayingCardException

让构造函数抛出异常不是一个好主意。

你确定构造函数中的代码会抛出这个异常吗?

尝试将此代码移动到另一个可以在您的主代码中调用的方法。

关于java - 扑克游戏中未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20559617/

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