首页 > 代码库 > 人机猜拳

人机猜拳


import java.util.Scanner;

public class menu {
    Scanner input=new Scanner(System.in);
    String name;
    String name1;
    int score;
    int num;
    int count=0;
    String ff;
    String ff1;
    int rf=0;
    int df=0;
    public void showFist(){
        System.out.println("出拳规则:1.剪刀2.石头3.布");
        System.out.print("请选择对方角色(1:刘备2:孙权3:曹操):");
        num=input.nextInt();
        System.out.print("请输入你的名字:");
        name=input.next();
        switch(num){
            case 1:
                name1="刘备";
                break;
            case 2:
                name1="孙权";
                break;
            case 3:
                name1="曹操";
                break;
            default:
                System.out.println("输出错误!重新输入!");
                showFist();
        }
        System.out.println(name+"\tVS\t"+name1+"\t对战");
    }
    public void showComput(){
        
        System.out.println("请出拳:1.剪刀2.石头3.布(输入相应数字):");
        num=input.nextInt();
        switch(num){
        case 1:
            ff="剪刀";
            break;
        case 2:
            ff="石头";
            break;
        case 3:
            ff="布";
            break;
        default:
            System.out.println("输出错误!重新输入!");
            showComput();
    }
        int i=(int)(Math.random()*3+1);
        switch(i){
        case 1:
            ff1="剪刀";
            break;
        case 2:
            ff1="石头";
            break;
        case 3:
            ff1="布";
            break;
        
    }
        System.out.println("你出拳:"+ff);
        System.out.println(name1+"出拳:"+ff1);
        if(num==1&&i==3||num==2&&i==1||num==3&&i==2){
            System.out.println("结果:恭喜,你赢了");
            rf=rf+1;
        }else if(num==1&&i==1||num==2&&i==2||num==3&&i==3){
            System.out.println("结果:和局,真衰!嘿嘿,等着瞧!");
            
        }else{
            System.out.println("结果:^_^,你输了,真笨!");
            df=df+1;
        }
        count=count+1;
        System.out.println("\n");
        System.out.println("是否进入下一轮(y/n):");
        String zm=input.next();
        while (zm.equals("y")) {
            showComput();
        }
        showjg();
    }
    public void showjg(){
        System.out.println("\n");
        System.out.println("--------------------------------------------------");
        System.out.println(name+"\tVS\t"+name1);
        System.out.println("对战次数:"+count);
        System.out.println("\n");
        System.out.println("姓名\t\t得分");
        System.out.println(name+"\t\t"+rf);
        System.out.println(name1+"\t\t"+df);
        System.out.println("\n");
        if(rf>df){
            System.out.println("恭喜恭喜!");
        }else if(rf<df){
            System.out.println("别灰心!继续加油!");
        }else if(rf==df){
            System.out.println("平局!");
        }

    }
}

 

import java.util.Scanner;

public class game {
     public static void main(String[] args) {
           Scanner input = new Scanner(System.in);
           String zn = "y";
           menu a = new menu();
          System.out.println("-----------------欢迎进入游戏世界--------------------");
          System.out.println("\n");
    System.out.println("\t\t******************");
    System.out.println("\t\t****猜拳,开始****");
    System.out.println("\t\t******************");
    System.out.println("\n");
    a.showFist();
    System.out.println("\n");
    System.out.print("要开始吗?(y/n)");
    zn = input.next();
    do{
                    
        a.showComput();
    }while(!zn.equals("y"));

    }

}

 

人机猜拳