gpt4 book ai didi

java - 我似乎无法在我的主体中创建任何对象?

转载 作者:行者123 更新时间:2023-11-30 08:02:43 26 4
gpt4 key购买 nike

该程序的重​​点是显示扑克牌和桥牌。到目前为止,当我运行程序时,我可以让它显示扑克描述,然后退出。我希望它继续显示扑克牌,然后显示桥牌描述和桥牌牌。我觉得我的问题来自抽象 DeckOfCards 类,但我真的不确定。

我将所有类放在同一个文件中,以便在编程时更容易遵循和编辑。

当程序失败时,它会给出这些错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52
at DeckOfCards.creatCardDeck(PlayCardGames.java:106)
at DeckOfCards.DeckOfCards(PlayCardGames.java:95)
at PlayCardGames.main(PlayCardGames.java:14)

这是程序代码本身(感谢您对其进行编辑以使其格式更好)

    import javax.swing.*;
import java.util.Random;

public class PlayCardGames {

public static void main(String[] args) {


Poker playPoker = new Poker();
playPoker.DeckOfCards();
playPoker.displayDescription();
playPoker.deal();

Bridge playBridge = new Bridge();
playBridge.DeckOfCards();
playBridge.displayDescription();
playBridge.deal();


}

}

//--------------------------------------------------------------------------------\\

interface DeckConstants{
final static int CARDS_IN_SUIT = 13;
final static int SUITS_IN_DECK = 4;
final static int CARDS_IN_DECK = CARDS_IN_SUIT * SUITS_IN_DECK;
}

//================================================================================\\

class Card{

private String suit;
private String rank;
private int rankIndex;

public Card(int suitIndex, int rankIndex){
setSuit(suitIndex);
setRank(rankIndex);
}

public String getSuit() {
if (suit.equalsIgnoreCase("Hearts")){ suit = "\u2665";}
else if (suit.equalsIgnoreCase("Diamonds")) { suit = "\u2666"; }
else if (suit.equalsIgnoreCase("Clubs")) { suit = "\u2663"; }
else if(suit.equalsIgnoreCase("Spades")) { suit = "\u2660"; }

return suit;
}

public void setSuit(int suitIndex) {
if (suitIndex == 1) {suit = "Spades";}
else if (suitIndex == 2) {suit = "Hearts";}
else if (suitIndex == 3) {suit = "Diamonds";}
else if(suitIndex == 4) {suit = "Clubs";}

}

public String getRank() {
if (rankIndex == 1) {rank = "Ace";}
else if (rankIndex == 11) {rank = "Jack";}
else if (rankIndex == 12) {rank = "Queen";}
else if(rankIndex == 13) {rank = "King";}

return rank;
}

public void setRank(int rankIndex) {
if (this.rankIndex >= 13){
this.rankIndex = 13;}
else if(this.rankIndex <= 1) {
this.rankIndex = 1;}

}

public String toString(String rank, String suit) {
return getRank() +" of " + getSuit();
}

}

//=================================================================================\\

abstract class DeckOfCards implements DeckConstants{

protected Card[] deck = new Card[CARDS_IN_DECK];

public void DeckOfCards(){
creatCardDeck();
shuffle(deck);
}

public void creatCardDeck() {
int numberOfCards = 0;

for (int suitCounter = 1; suitCounter < CARDS_IN_SUIT; suitCounter++)
{
for (int rankCounter = 1; rankCounter < CARDS_IN_SUIT; rankCounter++)
{
deck[numberOfCards] = new Card(suitCounter, rankCounter);
numberOfCards++;
}
}
}

public void shuffle(Card[] temp){
Random rnd = new Random();

for (int k = temp.length; k > 1; k--){
int i = k - 1;
int j = rnd.nextInt(k);

Card tmp = temp[i];
temp[i] = temp[j];
temp[j]= tmp;

}
}

public abstract void displayDescription();

public abstract void deal();

}

//===============================================================================\\

class Poker extends DeckOfCards{
private int cardsDealt = 5;
private int index = 0;

public void Poker(){
displayDescription();
deal();
}

public void displayDescription(){
String desc = "In poker, players bet on hands" +
"\n Winner can bluff or must have the highest hand if called";

JOptionPane.showMessageDialog(null, desc);

}

public void deal() {

String message = "Your Poker hand:\n";

for (int x = index; x < cardsDealt; x++){
message += deck[index] + "\n";
index++;
}

if (index == CARDS_IN_DECK){
shuffle(deck);
index = 0;
}

JOptionPane.showMessageDialog(null, message);
}


}

//===============================================================================\\

class Bridge extends DeckOfCards{
private int cardsDealt = 13;
private int index = 0;

public void Bridge(){
displayDescription();
deal();
}

public void displayDescription(){
String desc = "In bride, partners bid on how many tricks they will take." +
"\n The high bid determines a trump suit";

JOptionPane.showMessageDialog(null, desc);

}

public void deal() {
String message = "Your Bridge hand:\n";

for (int x = index; x < cardsDealt; x++){
message += deck[index] + "\n";
index++;
}

if (index == CARDS_IN_DECK){
shuffle(deck);
index = 0;
}
JOptionPane.showMessageDialog(null, message);

}

}

最佳答案

你知道吗,我认为这非常简单:这是您的代码:

for (int suitCounter = 1; suitCounter < CARDS_IN_SUIT; suitCounter++)
{
for (int rankCounter = 1; rankCounter < CARDS_IN_SUIT; rankCounter++)
{

我认为也许应该是:

for (int suitCounter = 1; suitCounter < SUITS_IN_DECK; suitCounter++)
{
for (int rankCounter = 1; rankCounter < CARDS_IN_SUIT; rankCounter++)
{

关于java - 我似乎无法在我的主体中创建任何对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642303/

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