扑克牌作业 我不知道哪里出问题了 大家能看看吗

来源:7-1 简易扑克牌游戏

没有人陪伴的星空4211314

2019-07-26 20:38

package pkpxm;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;




//1·创建一副扑克牌(包括各种花色)

//2·创建俩名玩家(id、name、手牌(手牌为扑克牌的集合))

//3.洗牌(将扑克牌)

//4·发牌(将洗牌之后的扑克牌集合,从第一张开始发给俩名玩家,按照一人一张的方式,每人发俩张)

//5.游戏(取手中最大的进行比较,大的赢,相等比较花色)


//创建一个扑克牌游戏的类

public class GamePoker {

//创建扑克牌、人物集合

List<Poker> pokerList;

List<Player> playerList;

//玩家

Player player1;

Player player2;

String []pokerType = {"红桃","黑桃","方片","梅花"};

String []pokerNum  = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"} ;

public GamePoker(){

pokerList = new ArrayList<Poker>();

playerList =new ArrayList<Player>();

}

//创建扑克牌

public void creatPoker(){

System.out.println("--------创建扑克牌...---------");

System.out.println("--------创建扑克牌成功!--------");

System.out.print("为:");

for (int i = 0; i < pokerType.length; i++) {

for (int j = 0; j < pokerNum.length; j++) {

System.out.print(pokerType[i] + pokerNum[j]);

pokerList.add(new Poker(pokerType[j],pokerNum[i]));

}

}

}

//洗牌

public void washPoker(){

System.out.println("开始洗牌");

Collections.shuffle(pokerList);    //进行随机打乱

System.out.println("洗牌结束");

}

//创建玩家

public void createPlayer(){

System.out.println("---------创建玩家---------");

Scanner console =new Scanner(System.in);

System.out.println("请输入第一位玩家ID:");

System.out.print("ID为:");

String id = console.next();

System.out.println("请输入第一位玩家姓名:");

System.out.println("姓名为:");

String name = console.next();

Player player1 = new Player(id, name);

playerList.add(player1);

System.out.println("---------创建玩家---------");

System.out.println("请输入第二位玩家ID:");

System.out.print("ID为:");

String id2 = console.next();

System.out.println("请输入第二位玩家姓名:");

System.out.println("姓名为:");

String name2 = console.next();

Player player2 = new Player(id2, name2);

playerList.add(player2);

System.out.println("欢迎玩家:" + player1.getName());  

System.out.println("欢迎玩家:" + name2);

}

//发牌

public void sendPoker(){

System.out.println("-------开始发牌--------");

System.out.println("玩家" + player1.getName() + "拿牌");

player1.handPokerList.add(pokerList.get(0));

System.out.println("玩家" + player2.getName() + "拿牌");

player2.handPokerList.add(pokerList.get(1));

System.out.println("玩家" + player1.getName() + "拿牌");

player1.handPokerList.add(pokerList.get(2));

System.out.println("玩家" + player2.getName() + "拿牌");

player2.handPokerList.add(pokerList.get(3));

System.out.println("------发牌结束------");

}

//游戏

public void createGame(){

System.out.println("---------开始游戏----------");

Collections.sort(player1.handPokerList);

Collections.sort(player2.handPokerList); 

Poker playerMax1= player1.handPokerList.get(1);

Poker playerMax2 = player2.handPokerList.get(1);

if (playerMax1.getRank() > playerMax2.getRank()) {

System.out.println(player1.getName() + "最大的手牌为:" + playerMax1);

}

else if (playerMax1.getRank() > playerMax2.getRank()) {

System.out.println(player2.getName() + "最大的手牌为:" + playerMax2);

}

else {

System.out.println("平局");

}

System.out.println("玩家各自的手牌为:");

player1.printPlayerPoker();

player2.printPlayerPoker();

}

public static void main(String[] args){

GamePoker gp = new GamePoker();

gp.creatPoker();

gp.washPoker();

gp.createPlayer();

gp.sendPoker();

gp.createGame();

}


}

package pkpxm;



//扑克类

public class Poker implements Comparable<Poker>{

public String Type;        //扑克数字

public String Num;       //扑克类型

private int Rank;         //越大等级越高

public Poker(String Type,String Num){

this.Type = Type;

this.Num = Num;

}

public int getRank(){

return Rank;

}

public int compareTo(Poker arg0) {

// TODO Auto-generated method stub

return this.Num.compareTo(Num);

}


}

package pkpxm;


import java.util.ArrayList;

import java.util.List;



//玩家类

public class Player {

private String id;

private String name;

List<Poker> handPokerList;     //存放手牌

public Player(String id,String name){

this.id = id;

this.id = name;

handPokerList = new ArrayList<Poker>();

}

public String getID(){

return id;

}


public String getName() {

// TODO Auto-generated method stub

return name;

}

//玩家最后的手牌

public void printPlayerPoker(){

for (Poker poker:handPokerList) {

System.out.print(name);

System.out.println(poker.Type + poker.Num);

}

}


}


运行之后就这样了:

--------创建扑克牌...---------

--------创建扑克牌成功!--------

为:红桃2红桃3红桃4红桃5红桃6Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

at pkpxm.GamePoker.creatPoker(GamePoker.java:41)

at pkpxm.GamePoker.main(GamePoker.java:120)


写回答 关注

1回答

  • franzhe
    2019-08-11 18:43:29

    pokerList.add(new Poker(pokerType[j],pokerNum[i]));   // i,j 位置换下 

    public Player(String id,String name){

    this.id = id;

    this.id = name;   //这里也不对

    handPokerList = new ArrayList<Poker>();

    }

    createGame 方法 里的比较似乎不对

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题