• 02_人机猜拳


    package com.renjicaiquan.entity;
    
    public class Computer {
        private String name;
        private int score;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getScore() {
            return score;
        }
        public void setScore(int score) {
            this.score = score;
        }
        public Computer(String name, int score) {
            super();
            this.name = name;
            this.score = score;
        }
        public Computer() {
            super();
        }
        @Override
        public String toString() {
            return "Computer [name=" + name + ", score=" + score + "]";
        }
        public int doGame(){
            int x=(int) (Math.random()*3+1);
            switch(x){
            case 1:
                System.out.println(this.getName()+"出拳:剪刀");
                break;
            case 2:
                System.out.println(this.getName()+"出拳:石头");
                break;
            case 3:
                System.out.println(this.getName()+"出拳:布");
                break;
            }
            return x;
        }
    
    }
    
    package com.renjicaiquan.entity;
    
    import java.util.Scanner;
    
    public class Game {
        private User user=new User();
        private Computer computer=new Computer();
        private int count;
        static Scanner input=new Scanner(System.in);
        public void welcome(){
            System.out.println("-----------------------欢迎进入游戏------------------------");
            System.out.println("		***********************");
            System.out.println("		**  猜拳,开始  **");
            System.out.println("		***********************");
        }
        public void start(){
            welcome();
            System.out.println("
    出拳规则:1、剪刀 2、石头 3、布");
            System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
            int check_1=getIntOneToThree();
            switch(check_1){
            case 1:
                computer.setName("刘备");
                break;
            case 2:
                computer.setName("孙权");
                break;
            case 3:
                computer.setName("曹操");
                break;
            }
            System.out.print("请输入你的名字:");
            user.setName(input.next());
            System.out.println(user.getName()+"	VS	"+computer.getName()+"
    ");
            
            System.out.println("要开始吗(y/n)?");
            String answer=getYorN();    
            if("n".equals(answer)){
                System.out.println("谢谢使用!");
                System.exit(0);
            }
            String isAgain="";
            do{
                int a=user.doGame();
                int b=computer.doGame();
                compareUserAndComputer(a, b);
                System.out.print("
    是否继续开始下一轮(y/n):");
                isAgain=getYorN();
            }while(isAgain.equals("y"));
            showResult();
        }
        public static int getIntOneToThree(){
            while(!input.hasNext("[1,2,3]{1}")){
                input=new Scanner(System.in);
                System.out.println("输入有误,请重新输入!");
            }
            return input.nextInt();
        }
        public void setUser(User user) {
            this.user = user;
        }
        public User getUser() {
            return user;
        }
        public void setComputer(Computer computer) {
            this.computer = computer;
        }
        public Computer getComputer() {
            return computer;
        }
        public void compareUserAndComputer(int a,int b){
            //a表示人出的拳 b表示电脑出的拳
            int x=a-b;
            this.setCount(this.getCount()+1);
            switch(x){
            case 1:
            case -2:        
                user.setScore(user.getScore()+1);
                System.out.println("结果:恭喜,你赢了!");
                break;
            case -1:
            case 2:
                computer.setScore(computer.getScore()+1);
                System.out.println("结果:^_^,你输了,真笨!");
                break;
            default:
                System.out.println("结果:平局!");    
            }
        }
        public void setCount(int count) {
            this.count = count;
        }
        public int getCount() {
            return count;
        }
        public void showResult(){
            System.out.println("--------------------------------");
            System.out.println(user.getName()+"	VS	"+computer.getName());
            System.out.println("对战次数:"+this.getCount());
            System.out.println("
    姓名	得分");
            System.out.println(user.getName()+"	"+user.getScore());
            System.out.println(computer.getName()+"	"+computer.getScore()+"
    ");
            if(user.getScore()>computer.getScore()){
                System.out.println(computer.getName()+":这不可能!");
            }else if(user.getScore()==computer.getScore()){
                System.out.println(computer.getName()+":再战300回合!");
            }else{
                System.out.println(computer.getName()+":我"+computer.getName()+"是无敌的,哈哈哈...");
            }
            System.out.println("--------------------------------");
            
        }
        public static String getYorN(){
            while(!input.hasNext("[y,n]{1}")){
                input=new Scanner(System.in);
                System.out.println("输入有误,请重新输入!");
            }
            return input.next();
        }
    
    }
    
    package com.renjicaiquan.entity;
    
    public class User {
        private String name;
        private int score;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getScore() {
            return score;
        }
        public void setScore(int score) {
            this.score = score;
        }
        public User(String name, int score) {
            super();
            this.name = name;
            this.score = score;
        }
        public User() {
            super();
        }
        @Override
        public String toString() {
            return "User [name=" + name + ", score=" + score + "]";
        }
        
        public int doGame(){
            System.out.println("你出拳:");
            int x=Game.getIntOneToThree();
            switch(x){
            case 1:
                System.out.println("你出拳:剪刀");
                break;
            case 2:
                System.out.println("你出拳:石头");
                break;
            case 3:
                System.out.println("你出拳:布");
                break;
            }
            return x;
        }
        
    }
    
    package com.renjicaiquan.test;
    
    import com.renjicaiquan.entity.Game;
    
    public class TestGame {
        public static void main(String[] args){
            Game g=new Game();
            g.start();
        }
    }
  • 相关阅读:
    便利的开发文档工具doxygen
    父页面 js 取得弹出窗口所选择的值, 弹出窗口关闭后刷新父页面
    ASCII码对照表
    C#中Brush、Color、String相互转换
    C#获取标准北京时间
    2005数据库脚本在SQL2000上执行 注意事项
    给Image控件后台赋Source值
    Web服务枚举组件不可用
    泛型集合转化为DataSet
    网站常见关于"登录|注册"和"姓名|注销"用JS实现
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5572446.html
Copyright © 2020-2023  润新知