扑克牌游戏关于玩家获得手牌出现空指针异常
代码我放到下面去了
为什么执行到p1.cards.add(c1);就空指针异常了?
谢谢,求解答
代码我放到下面去了
为什么执行到p1.cards.add(c1);就空指针异常了?
谢谢,求解答
2016-09-29
package com.poker;
import java.util.HashSet;
import java.util.Set;
public class Player {
public String name;
public int id;
public Set<Card> cards;
public Player(int id,String name){
this.id=id;
this.name=name;
this.cards = new HashSet<Card>();
}
public Player(){
}
}
set集合是没有get方法的,你放进去是取不到值得,所以一直都是null
package com.poker; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.Scanner; public class Playing { public List<Card> cardsToSelect; //没有洗牌的序列 public List<Card> washedList; //洗牌后的序列 public Scanner input = new Scanner(System.in); public Random random = new Random(); Player p1 = new Player(); Player p2 = new Player(); public Playing() { this.cardsToSelect = new ArrayList<Card>(); } //把牌添加到cardsToSelect序列中 public void addCard() { Card[] heitao = { new Card("黑桃", "A"), new Card("黑桃", "2"), new Card("黑桃", "3"), new Card("黑桃", "4"), new Card("黑桃", "5"), new Card("黑桃", "6"), new Card("黑桃", "7"), new Card("黑桃", "8"), new Card("黑桃", "9"), new Card("黑桃", "10"), new Card("黑桃", "J"), new Card("黑桃", "Q"), new Card("黑桃", "K") }; Card[] hongtao = { new Card("红桃", "A"), new Card("红桃", "2"), new Card("红桃", "3"), new Card("红桃", "4"), new Card("红桃", "5"), new Card("红桃", "6"), new Card("红桃", "7"), new Card("红桃", "8"), new Card("红桃", "9"), new Card("红桃", "10"), new Card("红桃", "J"), new Card("红桃", "Q"), new Card("红桃", "K") }; Card[] meihua = { new Card("梅花", "A"), new Card("梅花", "2"), new Card("梅花", "3"), new Card("梅花", "4"), new Card("梅花", "5"), new Card("梅花", "6"), new Card("梅花", "7"), new Card("梅花", "8"), new Card("梅花", "9"), new Card("梅花", "10"), new Card("梅花", "J"), new Card("梅花", "Q"), new Card("梅花", "K") }; Card[] fangkuai = { new Card("方块", "A"), new Card("方块", "2"), new Card("方块", "3"), new Card("方块", "4"), new Card("方块", "5"), new Card("方块", "6"), new Card("方块", "7"), new Card("方块", "8"), new Card("方块", "9"), new Card("方块", "10"), new Card("方块", "J"), new Card("方块", "Q"), new Card("方块", "K") }; cardsToSelect.addAll(Arrays.asList(heitao)); cardsToSelect.addAll(Arrays.asList(hongtao)); cardsToSelect.addAll(Arrays.asList(meihua)); cardsToSelect.addAll(Arrays.asList(fangkuai)); System.out.println("==========创建扑克牌=========="); System.out.println("==========创建扑克牌成功=========="); } //利用循环把添加的牌打印出来 public void showCard() { int size = cardsToSelect.size(); System.out.println("==========添加了如下扑克牌=========="); for (int i = 0; i < size; i++) { System.out.print(cardsToSelect.get(i).type + cardsToSelect.get(i).num + ","); } System.out.println(); } //创建两个玩家 public void creatPlayer() { while (true) { try { System.out.println("输入第一个玩家的ID:"); p1.id = input.nextInt(); System.out.println("输入第一个玩家的姓名:"); p1.name = input.next(); System.out.println("输入第二个玩家的ID:"); p2.id = input.nextInt(); System.out.println("输入第二个玩家的姓名:"); p2.name = input.next(); break; } catch (Exception e) { System.out.println("输入有误,请重试"); input = new Scanner(System.in); } } System.out.println("欢迎玩家:"+p1.name+","+p2.name); } public void washAndGetCard() { int size = cardsToSelect.size();//获得cardsToSelect序列的长度 List<Card> washedList = new ArrayList<Card>();//生成一个新的序列存放无序的牌 for (int i = 0; i < size; i++) { Card temp = new Card(); do { temp = cardsToSelect.get(random.nextInt(size } while (washedList.contains(temp)); washedList.add(temp);));//利用生成随机数在原本的序列中随机抽取不同的牌添加到新的序列中去 } Card c1=washedList.get(0);//因为washedList里面的牌是无序的 Card c2=washedList.get(1);//所以直接取前四张牌 Card c3=washedList.get(2); Card c4=washedList.get(3); p1.cards.add(c1);//执行到这里就报空指针异常 p2.cards.add(c2); p1.cards.add(c3); p2.cards.add(c4); System.out.println("玩家"+p1.name+"获得牌:"+c1.type+c1.num+","+c3.type+c3.num); System.out.println("玩家"+p2.name+"获得牌:"+c2.type+c2.num+","+c4.type+c4.num); } public static void main(String[] args) { Playing t = new Playing(); t.addCard(); t.showCard(); t.creatPlayer(); t.washAndGetCard(); } }
//Card类 package com.poker; public class Card { public String num; public String type; public Card(String type, String num) { this.num = num; this.type = type; } public Card(){ } }
//Player类 package com.poker; import java.util.HashSet; import java.util.Set; public class Player { public String name; public int id; public Set<Card> cards; public Player(int id,String name){ this.id=id; this.name=name; this.cards = new HashSet<Card>(); } public Player(){ } }
举报