手记

JAVA入门第三季代码:简易扑克牌游戏

引用文字
自己按照老师给的要求,尝试写了一下代码;可能不够标准,但老师说的要求我的代码都能,请各位大神多多指教!

package com.imooc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class PUke {
    public List<String> set;
    public Map<Integer,String>  player;
    public List<Integer> num;
    public int ID;
    public int ID1;
    public String name;
    public Scanner in=new Scanner(System.in);
public PUke(){
    this.set=new ArrayList<String>();
     this.player=new HashMap<Integer,String>();
     this.num=new ArrayList<Integer>();
  }
public void test(){

    System.out.println("---------创建扑克牌------------");
    System.out.println("---------扑克牌创建成功-----------");
    String[] suit={"方块","梅花","红桃","黑桃"};
    String[] num={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    int i=0;
    while(i<4){
    for(int j=0;j<13;j++){
    set.add(suit[i]+num[j]);
    }
    i++;
}
    System.out.print("为:[");
    for(String a:set){
    System.out.print(a+",");
}
    System.out.print("]");
}
public void test1(){
    System.out.println("----------创建玩家------------");
    System.out.println("请输入第1位玩家的ID和姓名:");
    System.out.println("输入ID:");
    }
public  void test1exception1(){
    try{
        ID=in.nextInt();
          }catch(InputMismatchException e){
        System.out.println("输入命令错误!请重新输入ID:");
        in=new Scanner(System.in);
        test1exception1();
        }
    }
public void test2(){
    if(player.get(ID)==null){
    System.out.println("输入玩家姓名:");
    String name=in.next(); 
    player.put(ID,name);
    }
    System.out.println("请输入第2位玩家的ID和姓名:");
    System.out.println("输入ID:");
    }
public  void test1exception2(){
     try{
            ID1=in.nextInt();
        }catch(InputMismatchException e){
            System.out.println("输入命令错误!请重新输入ID:");
            in=new Scanner(System.in);          
            test1exception2();
        }
     }
public void test3(){
    if(player.get(ID1)==null){
        System.out.println("输入玩家姓名:");
        String name=in.next(); 
        player.put(ID,name);
        }
    else{
    do{
    System.out.println("该玩家已存在!");
    System.out.println("请重新输入ID:");
    ID1=in.nextInt(); 
    }while(player.get(ID1)!=null);
    }
    System.out.println("输入玩家姓名:");
    String name=in.next(); 
    player.put(ID1,name);
    System.out.println("-----欢迎玩家:"+player.get(ID));
    System.out.println("-----欢迎玩家:"+player.get(ID1));       
}
public void test4(){
    int i;
    String e,f;
    String[]pukepai=new String[4];
    Random random=new Random();
    for(int j=0;j<4;j++){
    do{ i=random.nextInt(50);
    }while(num.contains(i));
    num.add(i);
    pukepai[j]=set.get(i);
    }
    System.out.println("-------开始发牌...--------");
    System.out.println("----玩家:"+player.get(ID)+"-拿牌");
    String a=pukepai[0];
    System.out.println("----玩家:"+player.get(ID1)+"-拿牌");
    String b=pukepai[1];
    System.out.println("----玩家:"+player.get(ID)+"-拿牌");
    String c=pukepai[2];
    System.out.println("----玩家:"+player.get(ID1)+"-拿牌");
    String d=pukepai[3];
    String[] player1={a,c};
    String[] player2={b,d};
    System.out.println("-------发牌结束!--------");
    System.out.println("-------开始游戏...--------");
    if(set.indexOf(player1[0])>set.indexOf(player1[1]))
            {
         e=player1[0];
    System.out.println("玩家:"+player.get(ID)+"最大的手牌为:"+e);
    }
    else{
         e=player1[1];
         System.out.println("玩家:"+player.get(ID)+"最大的手牌为:"+e);
    }
    if(set.indexOf(player2[0])>set.indexOf(player2[1])){
         f=player2[0];
        System.out.println("玩家:"+player.get(ID1)+"最大的手牌为:"+f);
        }
    else{
         f=player2[1];
        System.out.println("玩家:"+player.get(ID1)+"最大的手牌为:"+f);
        }
     if(set.indexOf(e)>set.indexOf(f)){
         System.out.println("-------玩家:"+player.get(ID)+"获胜!-------");
     }
     else{
     System.out.println("-------玩家:"+player.get(ID1)+"获胜!-------");
     }
     System.out.println("玩家各自的手牌为:");
     System.out.println(player.get(ID)+":["+player1[0]+","
     +player1[1]+"]");
     System.out.println(player.get(ID1)+":["+player2[0]+","
             +player2[1]+"]");
}
public static void main(String[] args){
    PUke pk=new PUke();
    pk.test();
    pk.test1();
    pk.test1exception1();
    pk.test2();
    pk.test1exception2();
    pk.test3();
    pk.test4();
}
}
12人推荐
随时随地看视频
慕课网APP

热门评论

这里稍微改了改:

public void test3(){ 

if(player.get(ID1)==null){ 

System.out.println("输入玩家姓名:"); 

String name=in.next(); 

player.put(ID1,name); 

}else{ 

do{ 

System.out.println("该玩家已存在!"); 

System.out.println("请重新输入ID:"); 

ID1=in.nextInt(); 

}while(player.get(ID1)!=null); 

System.out.println("-----欢迎玩家:"+player.get(ID)); 

System.out.println("-----欢迎玩家:"+player.get(ID1)); 

查看全部评论