首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

一副牌出去大小王,分给四个人。报java.lang.ArrayIndexOutOfBoundsException异常!

2014-01-26 
一副牌出去大小王,分给四个人。报java.lang.ArrayIndexOutOfBoundsException异常!import java.util.* publi

一副牌出去大小王,分给四个人。报java.lang.ArrayIndexOutOfBoundsException异常!

import java.util.*;
public class ApplycationCard
{
public static void main(String[] args)
{

Deck deckcards=new Deck();
deckcards.riffleCard();
deckcards.dealCard();

}
}
一个类;
import java.util.*;
public class Deck
{

public Card[] cards;
public Deck()

{
cards=new Card[52];
for(int i=0;i <4;i++)
{
for(int j=1;j <14;j++)
{
cards[i*13+j-1]=new Card(Suit.values()[i],Rank.values()[j]);
}
}
for(Card e:cards)
System.out.println(e.toString());
}

public void riffleCard()
{
Card[] tempCard=new Card[52];
for(int i=52;i>=1;i--)
{

int temp=(int)Math.round((Math.random()*100))%i;
tempCard[52-i]=cards[temp];
cards[temp]=cards[i-1];
}
for(int i=0;i <52;i++)
cards[i]=tempCard[i];

}


public void dealCard()
{
Card[] tempOne=new Card[13];
Card[] tempTwo=new Card[13];
Card[] tempThree=new Card[13];
Card[] tempFour=new Card[13];

for(int i=0;i <52;)
{
tempOne[i/4]=cards[i];
tempTwo[(i-1)/4]=cards[i+1];
tempThree[(i-2)/4]=cards[i+2];
tempFour[(i-3)/4]=cards[i+3];
i+=4;
}
for(Card e:tempOne)
System.out.println(e.toString());
for(Card e:tempTwo)
System.out.println(e.toString());
for(Card e:tempThree)
System.out.println(e.toString());
for(Card e:tempFour)
System.out.println(e.toString());

}
}
一个类;

import java.util.*;

enum Suit{CLUBS,SPADE,DIAMONDS,HEARTS}
enum Rank{Ace,Deuce,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King}


public class Card
{


public Suit suit;
public Rank rank;
public void Card()
{}

public Card(Suit newsuit,Rank newrank)
{
suit=newsuit;
rank=newrank;
}

public String toString()
{
return "The"+rank+"of"+suit+"s";
}

}
一个类;
运行,mian出错Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException,编译正常通过。我是新手,希望高手解答。(一副牌,import java.util.*;
public class ApplycationCard //应用类,主函数
{
public static void main(String[] args)
{

Deck deckcards=new Deck();
deckcards.riffleCard();
deckcards.dealCard();

}
}
一个类;
import java.util.*;
public class Deck //定义一个牌
{

public Card[] cards;
public Deck()

{
cards=new Card[52];
for(int i=0;i <4;i++)
{
for(int j=1;j <14;j++)
{
cards[i*13+j-1]=new Card(Suit.values()[i],Rank.values()[j]);
}
}
for(Card e:cards)
System.out.println(e.toString());
}

public void riffleCard() //洗牌
{
Card[] tempCard=new Card[52];
for(int i=52;i>=1;i--)
{

int temp=(int)Math.round((Math.random()*100))%i;
tempCard[52-i]=cards[temp];
cards[temp]=cards[i-1];
}
for(int i=0;i <52;i++)
cards[i]=tempCard[i];

}


public void dealCard() //发牌
{
Card[] tempOne=new Card[13];
Card[] tempTwo=new Card[13];
Card[] tempThree=new Card[13];
Card[] tempFour=new Card[13];

for(int i=0;i <52;)
{
tempOne[i/4]=cards[i];
tempTwo[(i-1)/4]=cards[i+1];
tempThree[(i-2)/4]=cards[i+2];
tempFour[(i-3)/4]=cards[i+3];
i+=4;
}
for(Card e:tempOne)
System.out.println(e.toString());
for(Card e:tempTwo)
System.out.println(e.toString());
for(Card e:tempThree)
System.out.println(e.toString());
for(Card e:tempFour)
System.out.println(e.toString());

}
}
一个类;

import java.util.*;

enum Suit{CLUBS,SPADE,DIAMONDS,HEARTS}
enum Rank{Ace,Deuce,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King}


public class Card //把花色和大小定义一个类
{


public Suit suit;
public Rank rank;
public void Card()
{}

public Card(Suit newsuit,Rank newrank)
{
suit=newsuit;
rank=newrank;
}

public String toString()
{
return "The"+rank+"of"+suit+"s";
}

}
一个类;
运行,mian出错Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException,编译正常通过。我是新手,希望高手解答。谢谢(一副牌,除去大小王,分给四个人)        

热点排行