做扑克牌游戏中遇到Exception in thread "main" java.lang.NumberFormatException: For input string: "K"
我先把13转成String类型,然后用Integer.valueof(string)转成int型,然后就报了这么个错误
相关代码如下
public player getWinner(player A,player B){
int maxA,maxB;
String colorA,colorB;
int countA,countB;
if(A.obj[0][1]==null||A.obj[1][1]==null||B.obj[0][1]==null||B.obj[1][1]==null) {return null;}
if(Integer.valueOf(A.obj[0][1])>=Integer.valueOf(A.obj[1][1])){
maxA = Integer.valueOf(A.obj[0][1]);
colorA = A.obj[0][0];
}else{
maxA = Integer.valueOf(A.obj[1][1]);
colorA = A.obj[1][0];
}
if(Integer.valueOf(B.obj[0][1])>=Integer.valueOf(B.obj[1][1])){
maxB =Integer.valueOf(B.obj[0][1]);
colorB =B.obj[0][0];
}else{
maxB =Integer.valueOf(B.obj[1][1]);
colorB =B.obj[1][0];
}
if(maxA>maxB){
return A;
}else if(maxA==maxB){
countA =colorCompare(colorB);
countB =colorCompare(colorB);
if(countA>countB){
return A;
}else{
return B;
}
}else{
return B;
}
}
public int colorCompare(String color){
int colorCount=0;
if("黑桃".equals(color)){
colorCount=4;
}else if("梅花".equals(color)){
colorCount=3;
}else if("方块".equals(color)){
colorCount=2;
}else if("红桃".equals(color)){
colorCount=1;
}
return colorCount;
}
public static void main(String[] args) {
puke pork = new puke();
ArrayList<card> list_card = (ArrayList<card>) pork.getCard();
System.out.println("请输入用户id");
Scanner s = new Scanner(System.in);
String ss = s.next();
System.out.println("请输入用户名");
Scanner n = new Scanner(System.in);
player personA = new player(ss, n.next());
player personB= new player("大神级", "大神");
System.out.println("用户创建完毕");
competition compe =new competition();
System.out.println("洗牌");
Collections.shuffle(list_card, new Random(10001));
// HashMap<Integer, String> set_card = compe.washCard(list_card);
// Object[] obj = compe.attributeCard(set_card);
for(card c:list_card){
if(Integer.valueOf(c.count)==1){
c.setCount("A");
}else if(Integer.valueOf(c.count)==11){
c.setCount("J");
}else if(Integer.valueOf(c.count)==12){
c.setCount("Q");
}else if(Integer.valueOf(c.count)==13){
c.setCount("K");
}
System.out.println(c.color+c.count);
}
System.out.println("start!发牌");
personA.obj[0][0] = list_card.get(0).color;
personA.obj[0][1] = list_card.get(0).count;
personB.obj[0][0] = list_card.get(1).color;
personB.obj[0][1] = list_card.get(1).count;
personA.obj[1][0] = list_card.get(2).color;
personA.obj[1][1] = list_card.get(2).count;
personB.obj[1][0] = list_card.get(3).color;
personB.obj[1][1] = list_card.get(3).count;
player winner = compe.getWinner(personA, personB);
System.out.println(personA.id+personA.name+"的牌:"
+personA.obj[0][0]+personA.obj[0][1]+personA.obj[1][0]+personA.obj[1][1]);
System.out.println(personB.id+personB.name+"的牌:"
+personB.obj[0][0]+personB.obj[0][1]+personB.obj[1][0]+personB.obj[1][1]);
System.out.println("获胜者id:"+winner.id+"获胜者用户名:"+winner.name);
}
}
扑克牌类如下
public class puke {
private List<card> list_card;
public List<card> getCard(){
list_card = new ArrayList<card>();
for(int i=1;i<14;i++){
for(int j=1;j<5;j++){
card c = new card();
c.setCount(String.valueOf(i));
if(j==1){
c.color="红桃";
}else if(j==2){
c.color="方块";
}else if(j==3){
c.color="黑桃";
}else if(j==4) {
c.color="梅花";
}
list_card.add(c);
}
}
return list_card;
}