黑马程序员技术交流社区
标题:
扑克
[打印本页]
作者:
邱峁
时间:
2012-11-12 18:41
标题:
扑克
public class PuKe
{
public class Card
{
static int HongTao = 1;
static int FangKuai = 2;
static int CaoHua = 3;
static int HeiTao = 4;
int point;
int type;
public Card()
{
point = 1;
type = HongTao;
}
public Card(int point, int type)
{
this.point = point;
this.type = type;
}
int getType(){
return type;
}
int getPoint(){
return point;
}
public void show(){
if(type==HongTao) System.out.print("红桃");
if(type==FangKuai) System.out.print("方块");
if(type==CaoHua) System.out.print("草话");
if(type==HeiTao) System.out.print("黑桃");
System.out.println(point);
}
}
Card[] cards;
public PuKe()
{
cards = new Card[52]; // 没有创建Card, 只是创建了数组
for(int i=0; i<13; i++){
cards[i] = new Card(i+1, Card.HeiTao);
}
for(int i=0; i<13; i++){
cards[i+13] = new Card(i+1, Card.HongTao);
}
for(int i=0; i<13; i++){
cards[i+13*2] = new Card(i+1, Card.CaoHua);
}
for(int i=0; i<13; i++){
cards[i+13*3] = new Card(i+1, Card.FangKuai);
}
}
public void xiPai()
{
for(int i=0; i<1000; i++){
int m = Math.random() ;
int n = Math.random() ;
Card t = cards[m];
cards[m] = cards[n];
cards[n] = t;
}
}
public void faPai(PuKe.Card[] t1, PuKe.Card[] t2, PuKe.Card[] t3, PuKe.Card[] t4)
{
for(int i=0; i<13; i++){
t1[i] = cards[i];
}
for(int i=0; i<13; i++){
t2[i] = cards[i+13];
}
for(int i=0; i<13; i++){
t3[i] = cards[i+13*2];
}
for(int i=0; i<13; i++){
t4[i] = cards[i+13*3];
}
}
public static void main(String[] args)
{
PuKe a = new PuKe();
a.xiPai();
PuKe.Card[] t1 = new PuKe.Card[13];
PuKe.Card[] t2 = new PuKe.Card[13];
PuKe.Card[] t3 = new PuKe.Card[13];
PuKe.Card[] t4 = new PuKe.Card[13];
a.faPai(t1, t2, t3, t4);
for(int i=0; i<t2.length; i++){
t2[i].show();
}
}
}
我想要13张不重复的牌
这是哪错了
作者:
方建平
时间:
2012-11-12 18:50
下面的代码有问题吧,Math.random()得到的是0到1.0之间小数啊!
int m = Math.random() ;
int n = Math.random() ;
作者:
方建平
时间:
2012-11-12 18:55
static int HongTao = 1;
static int FangKuai = 2;
static int CaoHua = 3;
static int HeiTao = 4;
此外在内部类中不能有static类成员吧
作者:
小灰灰
时间:
2012-11-12 19:02
本帖最后由 都彭韬 于 2012-11-12 19:04 编辑
public class PuKe {
Card[] cards;
public PuKe() {
cards = new Card[52]; // 没有创建Card, 只是创建了数组
for (int i = 0; i < 13; i++) {
cards[i] = new Card(i + 1, Card.HeiTao);
}
for (int i = 0; i < 13; i++) {
cards[i + 13] = new Card(i + 1, Card.HongTao);
}
for (int i = 0; i < 13; i++) {
cards[i + 13 * 2] = new Card(i + 1, Card.CaoHua);
}
for (int i = 0; i < 13; i++) {
cards[i + 13 * 3] = new Card(i + 1, Card.FangKuai);
}
}
public void xiPai() {
for (int i = 0; i < 1000; i++) {
int m = (int) Math.random();
int n = (int) Math.random();
Card t = cards[m];
cards[m] = cards[n];
cards[n] = t;
}
}
public void faPai(PuKe.Card[] t1, PuKe.Card[] t2, PuKe.Card[] t3, PuKe.Card[] t4) {
for (int i = 0; i < 13; i++) {
t1[i] = cards[i];
}
for (int i = 0; i < 13; i++) {
t2[i] = cards[i + 13];
}
for (int i = 0; i < 13; i++) {
t3[i] = cards[i + 13 * 2];
}
for (int i = 0; i < 13; i++) {
t4[i] = cards[i + 13 * 3];
}
}
public static void main(String[] args) {
PuKe a = new PuKe();
a.xiPai();
Card[] t1 = new Card[13];
Card[] t2 = new Card[13];
Card[] t3 = new Card[13];
Card[] t4 = new Card[13];
a.faPai(t1, t2, t3, t4);
for (int i = 0; i < t2.length; i++) {
t2[i].show();
}
}
class Card
{
static final int HongTao = 1;
static final int FangKuai = 2;
static final int CaoHua = 3;
static final int HeiTao = 4;
int point;
int type;
public Card()
{
point = 1;
type = HongTao;
}
public Card(int point, int type)
{
this.point = point;
this.type = type;
}
int getType(){
return type;
}
int getPoint(){
return point;
}
public void show(){
if(type==HongTao) System.out.print("红桃");
if(type==FangKuai) System.out.print("方块");
if(type==CaoHua) System.out.print("草话");
if(type==HeiTao) System.out.print("黑桃");
System.out.println(point);
}
}
}
复制代码
你的代码 问题有很多啊,我已经修复好了,你运行一下吧
运行成功了
内部类的静态变量要改错 static final的,还有就是
int m = (int) Math.random();
要转换一下吧
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2