CreatePlayer过程中,如果玩家ID有一次或多次输入的是非整数值 那在发牌后提示输出玩家手牌时,对应玩家的姓名会显示为null,为什么?
********玩家类**********
package demo8;
import java.util.*;
public class Player {
public String id;
public String name;
public List<Card> HandCards;
public Player(String id,String name){
this.id=id;
this.name=name;
HandCards=new ArrayList<Card>();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void getHandCards(Card c){
HandCards.add(c);
}
public void reSet(){
if(HandCards!=null){
HandCards.clear();
}
}
public Card getMax(){
Collections.sort(HandCards);
return HandCards.get(HandCards.size()-1);
}
public String toString(){
String str=new String();
for (Card c : HandCards) {
str+=c.toString();
}
return "玩家"+name+"---"+str;
}
}
*********卡牌类***********
package demo8;
public class Card implements Comparable<Card> {
int color;
int num;
public Card(int num,int color){
this.color=color;
this.num=num;
}
public int compareTo(Card o){
if(this.num>o.num)
return 1;
else if(this.num<o.num)
return -1;
else
return this.color-o.color;
}
public String toString(){
String[] cardNum={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String s=new String();
switch(color){
case 0:
s="方片";
break;
case 1:
s="梅花";
break;
case 2:
s="红桃";
break;
case 3:
s="黑桃";
break;
default:
s="花色错误";
break;
}
return s+cardNum[num];
}
}
**********test***************
package demo8;
import java.util.*;
public class Test {
public List<Card> Cards;
//创建扑克牌
public void createCard(){
Cards=new ArrayList<Card>();
System.out.println("------创建扑克牌--------");
for(int i=0;i<13;i++)
for(int j=0;j<4;j++)
Cards.add(new Card(i,j));
System.out.println("----遍历打印扑克牌资源----");
for (Card c : Cards) {
System.out.println(c);
}
}
//洗牌
public void wash(){
if(Cards==null){
System.out.println("----没有扑克牌,游戏结束-----");
return;
}
System.out.println("-----开始洗牌-----");
Collections.shuffle(Cards);
System.out.println("----遍历打印扑克牌资源----");
for (Card c : Cards) {
System.out.println(c);
}
}
//创建玩家方法
public Player CreatePlayer(){
String id=new String();
String name=new String();
Player player=new Player(id,name);
Scanner input=new Scanner(System.in);
try{
System.out.println("请输入玩家ID:");
String ID=input.next();
if(Integer.valueOf(ID)!=null){
player.setId(ID);
System.out.println("请输入玩家姓名:");
name=input.next();
player.setName(name);
}else
throw new Exception();
}catch(Exception e){
System.out.println("输入错误,请输入整数ID!");
CreatePlayer();
}
return player;
}
//开始游戏方法
public void playGame(Player player1,Player player2){
player1.reSet();
player2.reSet();
player1.getHandCards(Cards.get(0));
player2.getHandCards(Cards.get(1));
player1.getHandCards(Cards.get(2));
player2.getHandCards(Cards.get(3));
player1.getHandCards(Cards.get(4));
player2.getHandCards(Cards.get(5));
System.out.println("----打印玩家手牌-----");
System.out.println(player1);
System.out.println(player2);
System.out.println("-----打印玩家最大手牌------");
Card c=player1.getMax();
Card d=player2.getMax();
System.out.println(player1.getName()+"最大手牌为:"+c);
System.out.println(player2.getName()+"最大手牌为:"+d);
if(c.compareTo(d)>0)
System.out.println("赢家为:"+player1.getName());
else
System.out.println("赢家为:"+player2.getName());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=new Test();
System.out.println("------欢迎来到扑克牌游戏-------");
Player player1;
Player player2;
System.out.println("-----创建玩家1-----");
player1=test.CreatePlayer();
System.out.println("-----创建玩家2-----");
player2=test.CreatePlayer();
test.createCard();
test.wash();
test.playGame(player1,player2);
System.out.println("\n");
System.out.println("-------开始第二次游戏------");
System.out.println("\n");
test.wash();
test.playGame(player1, player2);
}
}