计划交易4应该创建一个洗牌的牌组,并从牌组中交出四手13张牌。
我在out of memory error
课程的cards[i] = Card(x);
处获得了DealFour
。我试图通过java -Xmx
参数增加最大堆空间。这不起作用。
这是Card类
package Casino;
@SuppressWarnings("unchecked")
public class Card extends Object
{
String[] deck = {"AH", "KH", "QH", "JH", "10H", "9H", "8H", "7H", "6H", "5H", "4H", "3H", "2H",
"AS", "KS", "QS", "JS", "10S", "9S", "8S", "7S", "6S", "5S", "4S", "3S", "2S",
"AD", "KD", "QD", "JD", "10D", "9D", "8D", "7D", "6D", "5D", "4D", "3D", "2D",
"AC", "KC", "QC", "JC", "10C", "9C", "8C", "7C", "6C", "5C", "4C", "3C", "2C"};
String card;
public Card(int i)
{
card = deck[i];
}
public void show()
{
System.out.println(card);
}
}
以下是DealFour
类:
import java.util.*;
import java.io.*;
import Casino.*;
@SuppressWarnings("unchecked")
public class DealFour
{
public static void main(String[] args)
{
Stack shuffledDeck = new Stack();
Random repeater = new Random();
Card[] cards = new Card[52];
int high = 52;
for(int i = 0; i < 52;)
{
int x = Math.abs(repeater.nextInt(52));
if(x<52)
{
cards[i] = new Card(x);
if(i==0)
{
shuffledDeck.push(cards[i]);
}
else
{
for(int w = 0; w < i;)
{
int q = (int)(i-1);
if(w==q)
{
shuffledDeck.push(cards[i]);
i++;
}
if(cards[w]!=cards[i])
{
w++;
}
}
}
}
}
Card[] hand1 = new Card[13];
Card[] hand2 = new Card[13];
Card[] hand3 = new Card[13];
Card[] hand4 = new Card[13];
System.out.println("Hand 1");
for(int x = 0; x < 13; x++)
{
hand1[x] = (Card)shuffledDeck.pop();
hand1[x].show();
hand2[x] = (Card)shuffledDeck.pop();
hand3[x] = (Card)shuffledDeck.pop();
hand4[x] = (Card)shuffledDeck.pop();
}
System.out.println("Hand 2");
for(int y = 0; y < 13; y++)
{
hand2[y].show();
}
System.out.println("Hand 3");
for(int z = 0; z < 13; z++)
{
hand3[z].show();
}
System.out.println("Hand 4");
for(int g = 0; g < 13; g++)
{
hand4[g].show();
}
}
}
答案 0 :(得分:3)
你创造了一个无限循环:
for (int i = 0; i < 52;) {
int x = Math.abs(repeater.nextInt(52)); // no need for Math.abs, this
// result is always positive
if (x < 52) { // note: this if is redundant
cards[i] = new Card(x);
if (i == 0) { // <--------------- this is always true
shuffledDeck.push(cards[i]);
} else {
// ...
}
}
}
你永远不会增加i
,所以循环永远不会结束。唯一的增量操作隐藏在else
子句中,永远不会执行。
我怀疑这是家庭作业或自学练习,所以我不会试图解决任何其他问题。希望这个暗示足以让你努力寻求解决方案。