zoukankan      html  css  js  c++  java
  • 人机猜拳

    import java.util.Scanner;
    
    public class Person {
         
        public int showFist() {
            Scanner input = new Scanner(System.in);
                System.out.println("请出拳:1.剪刀2.石头3.布(输入相应的数字)");
                int x = input.nextInt();
                switch (x) {
                case 1:
                    System.out.println("你出拳:剪刀");
                    break;
                case 2:
                    System.out.println("你出拳:石头");
                    break;
                case 3:
                    System.out.println("你出拳:布");
                    break;
                default:
                    System.out.println("输入错误");
                    break;
                }
                 return x;
    
            }
    
    
    }
    public class Computer{
        public int showFist(){
            int s=(int)(Math.random()*10%3+1);
            switch(s){
            case 1:
            System.out.println("电脑出拳:剪刀");
            break;
            case 2:
            System.out.println("电脑出拳:石头");
            break;
            case 3:
            System.out.println("电脑出拳:布");
            berak;
            default:
            System.out.println("输入错误");
            break;        
            }
            return s;
        }
    }
    import java.util.Scanner;
    
    public class Caiquan {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            Person o = new Person();
            Computer on = new Computer();
            int count = 0;
            int wo = 0;
            int computer = 0;
            System.out.println("------------------欢迎进入游戏世界---------------------");
            System.out.println("
    ");
            System.out.println("		***************		");
            System.out.println("		*** 猜拳,开始   ***		");
            System.out.println("		***************		");
            System.out.println("出拳规则:1.剪刀2.石头3.布");
            System.out.print("请选择对方角色(1:刘备2:孙权3:曹操):");
            int x = input.nextInt();
            String name1 = "";
                switch (x) {
                    case 1:
                        name1 = "刘备";
                        System.out.println("你选择了刘备对战");
                        break;
                    case 2:
                        name1 = "孙权";
                        System.out.println("你选择了孙权对战");
                        break;
                    case 3:
                        name1 = "曹操";
                        System.out.println("你选择了曹操对战");
                        break;
                }
                System.out.print("请输入你的姓名:");
                String name = input.next();
                System.out.println(name + "VS" + name1);
                String con = "";
                System.out.print("
    要开始码?(y/n):");
                con = input.next();
    
                while (con.equals("y")) {
                int perFist;
                int compFist;
                perFist = o.showFist();
                compFist = on.showFist();
                    if ((perFist==1 && compFist==1)    || (perFist==2 && compFist==2) || (perFist==3 && compFist==3)) {
                        System.out.println("结果:平局
    ");
    
                    } else if ((perFist==1 && compFist==3) || (perFist==2 && compFist==1) || (perFist==3 && compFist==2)) {
                        System.out.println("结果:你赢了");
                        wo++;
                    } else {
                        System.out.println("结果说:你输了
    ");
                        computer++;
                    }
                    count++;
                    System.out.print("
    是否开始下一轮(y/n):");
                    con = input.next();
                }
                System.out.println("------------------------------------------");
                System.out.println(name + "VS" + name1);
                System.out.println("对战次数:" + count);
                System.out.println("姓名		得分");
                System.out.println(name + "		" + wo);
                System.out.println(name1 + "		" + computer);
                if (computer < wo) {
                    System.out.println("结果:你赢了");
                } else if (computer == wo) {
                    System.out.println("结果:平局");
                } else if (computer > wo) {
                    System.out.println("结果:你输了");
                }
                    System.out.println("------------------------------------------");
            
            }
    
    }
  • 相关阅读:
    [51nod] 1088 最长回文子串 #Hash+二分
    [51nod] 1378 夹克老爷的愤怒 #树形DP
    [BZOJ] 2456: mode #众数计数法
    [51nod] 1199 Money out of Thin Air #线段树+DFS序
    [51nod] 1494 选举拉票 #算法设计策略
    [51nod] 1463 找朋友 #离线+扫描线
    [BZOJ] 2733: [HNOI2012]永无乡 #线段树合并+并查集
    [BZOJ] 1012: [JSOI2008]最大数maxnumber
    [Codeforces] E. Lomsat gelral #DSU on Tree
    [BZOJ] 4756: [Usaco2017 Jan]Promotion Counting #线段树合并+权值线段树
  • 原文地址:https://www.cnblogs.com/wxl941549541/p/7358661.html
Copyright © 2011-2022 走看看