我对java非常陌生,但我正在尝试修复这个堆栈溢出错误。基本上,我正在尝试使用main中的构造函数调用创建一组卡片: Deck newDeck = new Deck();
然后,在基类“deck”中创建包含52个元素(卡片)的卡片数组,并在每个索引处用两个元素填充数组 int
值,表示每张卡的等级和套装。
我相信堆栈溢出错误是由于基类和扩展类之间的递归构造函数调用造成的,尽管我可能错了。有没有人能提供一个关于如何修复这个错误的建议,或者以不同的方式填充cards数组?基类和扩展类格式对于我的项目是必需的。谢谢。
错误:
Exception in thread "main" java.lang.StackOverflowError
at java.base/java.util.Random.<init>(Random.java:105)
at Deck.<init>(Deck.java:12)
at Cards.<init>(Cards.java:19)
at Deck.<init>(Deck.java:27)
at Cards.<init>(Cards.java:19)
at Deck.<init>(Deck.java:27)
at Cards.<init>(Cards.java:19)
at Deck.<init>(Deck.java:27)
at Cards.<init>(Cards.java:19)
at Deck.<init>(Deck.java:27)
at Cards.<init>(Cards.java:19)
主要内容:
import java.util.Random;
import java.util.Scanner;
public class PokerProgram {
public static void main(String[] args) {
// CREATING SCANNER OBJECT
Scanner scnr = new Scanner(System.in);
Random random = new Random();
// INITIALIZING VARIABLES
String play = "y";
double blind = 10;
double playerBalance = 1000;
double playerBet;
double pot;
int playerScore = 0;
int compScore = 0;
String[] Hands = {"Equal hand", "One Pair", "Two Pair", "Three of a Kind", "Straight", "Flush", "Full House", "Four of a Kind", "Straight Flush", "Royal Flush"};
Deck newDeck = new Deck();
基类:
import java.util.Random;
public class Deck{
// Creating Random object
Random random = new Random();
// FIELD VARIABLES
public static int handScore;
public static int cardValue;
private static final Cards[] newDeck = new Cards[52];
// CONSTRUCTORS
// For Deck
public Deck () {
int i = 0;
for (int j = 0; j < 13; j++) {
for (int k = 0; k < 4; k++) {
newDeck[i] = new Cards(j, k);
i++;
}
}
}
扩展类:
public class Cards extends Deck{
// Field Variables
private int rank;
private int suit;
// Rank and Suit Arrays
private static String [] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
private static String [] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
// CONSTRUCTOR
public Cards (int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
1条答案
按热度按时间vlf7wbxs1#
牌可以延伸牌组。在创建一副牌时,你调用了cards构造器,而这个构造器恰好是调用cards构造器的一副牌。。。