• 猜拳游戏


    类图

    image.png

    源代码

    User类

    import java.util.Scanner;
    public class User extends Player{
        public String sayName(){
            System.out.println("请输入您的名字:");
            Scanner input = new Scanner(System.in);
            return input.next();
        }
        public int play(){//出拳
            System.out.println("请"+super.getName()+"出拳");
            Scanner input = new Scanner(System.in);
            int x = input.nextInt();
            while(true){
                if(x!=1&&x!=2&&x!=3) {
                    System.out.println("输入有误!请输入1-3的数字");
                     x = input.nextInt();
                }
                else break;
            }
            return x;
        }
    }
    
    

    Computer类

    import java.util.Random;
    public class Computer extends Player{
        public int play(){//出拳
            Random r = new Random();
            return r.nextInt(3)+1;
        }
    }
    

    Player类

    mport java.util.Scanner;
    public class Player {
        private String name;
        private int count;//标记赢的次数
        {
            count = 0;
            name = "张三";
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        public void setCount(int count) {
            this.count = count;
        }
    
        public int getCount() {
            return count;
        }
    
    }
    

    Judge

    import java.util.Scanner;
    public class Judge {
        public static String[] arr={"剪刀","石头","布"};
        public void print(){
            System.out.println("--------开始游戏--------");
            System.out.println("规则:1、石头 2、剪刀 3、布");
        }
        public String askName(Player player){
            return ((User)player).sayName();
        }
        public void playGame(Player player1,Player player2){
            for(int i=0;i<5;i++){
                System.out.println("--第"+(i+1)+"局游戏开始--");
                int r1 = ((User)player1).play();
                int r2 = ((Computer)player2).play();
                System.out.println(player1.getName()+"出"+arr[r1-1]);
                System.out.println(player2.getName()+"出"+arr[r2-1]);
                if((r1==1&&r2==2)||(r1==2&&r2==3)||(r1==3&&r2==1)) {
                    player2.setCount(player2.getCount()+1);
                    System.out.println(player2.getName()+"胜");
                }
                else if((r1==1&&r2==3)||(r1==2&&r2==1)||(r1==3&&r2==2)) {
                    player1.setCount(player1.getCount() + 1);
                    System.out.println(player1.getName()+"胜");
                }
                 else System.out.println("平局");
            }
    
        }
        public String showResult(Player player1,Player player2){
            System.out.println("--------最终结果--------");
            if(player1.getCount()>player2.getCount()) return player1.getName()+"胜利";
            else if(player1.getCount()<player2.getCount()) return player2.getName()+"胜利";
            else return "平局";
        }
        public static void main(String[] args) {
            Player player1 = new User();
            Player player2 = new Computer();
            Judge judge = new Judge();
            judge.print();
            player1.setName(judge.askName(player1));
            judge.playGame(player1,player2);
            System.out.println(judge.showResult(player1,player2));
    
        }
    }
    

    效果图

    image.png

    image.png
    image.png
    image.png
    image.png

    ljm要加油
  • 相关阅读:
    SpringCloud Eureka的一些问题
    git branch---删除命令
    go:cannot find main module;see 'go help modules'
    ERROR: yaml.scanner.ScannerError: while scanning for the next token found character ' ' that cannot
    vscode 打开wsl2:ubuntu中的文件进行编辑保存提示没有权限
    Ubuntu20.04 wechat和qq 字体太小,乱码 #
    ubuntu20.4 微信安装
    centos7中在环境变量中添加了GO的PATH,但依然无效
    直接把linux装在了物理机上
    win10 + linux 双系统安装
  • 原文地址:https://www.cnblogs.com/ljmmm1/p/14263883.html
Copyright © 2020-2023  润新知