首页 > 代码库 > 人机猜拳

人机猜拳

public class xm {
    Scanner input=new Scanner(System.in);
    String name;
    int dz;
public void yx(){
    System.out.println("------------------------------欢迎进入游戏世界------------------------------");
    System.out.println();
    System.out.println("\t\t\t*****************************");
    System.out.println("\t\t\t****\t猜拳,\t开始****");
    System.out.println("\t\t\t*****************************");
    System.out.println();
    
    System.out.println("出拳规则:1.剪刀2.石头3.布");
    System.out.println("请选择对方的角色(1:刘备2:孙权3:曹操):");
    int js=input.nextInt();
    System.out.println("请输入你的姓名:");
    String xm=input.next();
    switch (js){
    case 1:
        name="刘备";
        break;
    case 2:
        name="孙权";
        break;
    case 3:
        name="曹操";
        break;
    }
    String ks="y";
    int cq;
    System.out.println(xm+"\t"+"VS"+"\t"+name);
    System.out.println("要开始吗?(y/n)");
    ks=input.next();
    
    while (ks.equals("y")){
        System.out.println("请出拳:1。剪刀2.石头3.布(输入相应数字):");
        cq=input.nextInt();
        switch(cq){
        case 1:
            System.out.println("你出拳:剪刀");
            break;
        case 2:
            System.out.println("你出拳:石头");
            break;
        case 3:
            System.out.println("你出拳:布");
            break;
        }
        //System.out.println("你出拳:"+cq);
        int sj=(int)(Math.random()*3);
        sj++;
        switch(sj){
        case 1:
            System.out.println("系统出拳:剪刀");
            break;
        case 2:
            System.out.println("系统出拳:石头");
            break;
        case 3:
            System.out.println("系统出拳:布");
            break;
        }
        if((cq==1&&sj==1)||(cq==2&&sj==2)||(cq==3&&sj==3)){
            System.out.println("结果:平局");
        }else if((cq==1&&sj==3)||(cq==2&&sj==1)||(cq==3&&sj==2)){
            System.out.println("恭喜你,你赢了!");
            
        }else{
            System.out.println("你输了");
        }
        System.out.println("要开始吗?(y/n)");
        ks=input.next();
        dz++;
    }
    System.out.println("----------------------------------------------");
    System.out.println(xm+"\tVS\t"+name);
    System.out.println("对战次数:"+dz);
    System.out.println("----------------------------------------------");
}
}
public class xm$1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        xm a=new xm();
        a.yx();
    }

}

 

人机猜拳