• 人机猜拳


    import java.util.Scanner;
     public class Person {
        Scanner input = new Scanner(System.in);
           String name;
        int score;
        String action;
        int num;
          public void method() {
            System.out.println("
    请出拳:1.剪刀 2.石头 3.布");
            boolean a = true;
            do {
    
                num = input.nextInt();
                if (num == 1 || num == 2 || num == 3) {
                    switch (num) {
                    case 1:
                        action = "剪刀";
                        break;
                    case 2:
                        action = "石头";
                        break;
                    case 3:
                        action = "布";
                        break;
                    }
                    a = false;
                    System.out.println("你出拳:" + action);
                } else {
                    System.out.println("输入数字有误,请重新输入");
                }
    
            } while (a);
        }
    }

    第二个类,电脑类:

      public class Computer {
             String name;
        int score;
        String action;
        int num;
             public void method(){
                      num=(int)((Math.random())*3)+1;
            switch(num){
            case 1:
                action="剪刀";
                break;
            case 2:
                action="石头";
                break;
            case 3:
                action="布";
                break;
            }
            System.out.println(name+"出拳:"+action);
            
        }
    }
                                        

    第三个类,游戏类:

    import java.util.Scanner;
    
    public class Game {
        Scanner input = new Scanner(System.in);
            Person person = new Person();//创建玩家类的对象
        Computer computer = new Computer();//创建电脑类的对象
        int number;
        int frequency = 0;
            public void process() {
            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:曹操):");
            boolean b = true;
            do {
                number = input.nextInt();
                if (number == 1 || number == 2 || number == 3) {
                    switch (number) {
                    case 1:
                        computer.name = "刘备";
                        break;
                    case 2:
                        computer.name = "孙权";
                        break;
                    case 3:
                        computer.name = "曹操";
                        break;
    
                    }
                    b = false;
                } else {
                    System.out.println("输入数字有误,请重新输入");
                }
            } while (b);
            System.out.print("请输入你的姓名:");
            person.name = input.next();
            System.out.println(person.name + "  VS  " + computer.name + "  对战
    ");
            System.out.println("要开始吗?(y/n)");
            char answer = input.next().charAt(0);
            while (answer == 'y') {
                person.method();
                computer.method();
                if (person.num == computer.num) {
                    System.out.println("嘿嘿,和局,等着瞧吧!");
                } else if (((person.num == 2) && (computer.num == 1))
                        || (person.num == 1) && (computer.num == 3)
                        || ((person.num == 3) && (computer.num == 2))) {
                    System.out.println("哇,你赢了,好厉害!");
                    person.score++;
                } else {
                    System.out.println("^_^!!!你输了,真笨!");
                    computer.score++;
                }
                frequency++;
                System.out.println("
    ");
                System.out.println("还要继续吗?(y/n)");
                answer = input.next().charAt(0);
            }
        }
           public void showResult() {
            System.out.println("********************************");
            System.out.println(person.name + "  VS  " + computer.name);
            System.out.println("对战次数:" + frequency);
            System.out.println("
    姓名		得分");
            System.out.println(person.name + "		" + person.score);
            System.out.println(computer.name + "		" + computer.score);
            if (person.score < computer.score) {
                System.out.println("呵呵,笨笨,下次加油!");
            } else if (person.score == computer.score) {
                System.out.println("哇,竟然平局,我们下次一决胜负!");
            } else {
                System.out.println("哇,你好棒啊!");
            }
            System.out.println("********************************");
    
        }
    }

    测试类:

    public class Demo {
    public static void main(String[] args) {
        Game play=new Game();//创建游戏类的对象
        play.process();//调用游戏类的游戏进程方法
        play.showResult();//调用游戏类的结算方法
    }
    }
  • 相关阅读:
    windows下mysql数据库导入导出
    比较两个数组,根据id删除相同的对象
    angular子组件给父组件传值
    angular父组件给子组件传值
    angular获取dom节点
    angular创建服务
    forEach和for包含异步调用的区别
    用某种符号或字符替换某些字符
    嵌套函数和闭包
    JavaScript 递归
  • 原文地址:https://www.cnblogs.com/Chencheno/p/9789141.html
Copyright © 2020-2023  润新知