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

    第一个类:

    package Lianxi;

    import java.util.Scanner;

    public class Rjcq {
      Scanner input = new Scanner(System.in);

    //定义玩家类的属性

        String name;
        int score;
        String action;
        int num;

    //定义玩家类的猜拳方法

        public void show() {
          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);
          }

        }


    第二个类:

    package Lianxi

    public class Rjcq01 {        //电脑类

      String name;
      int score;
      String action;
      int sum;

    //定义电脑类猜拳方法

      public void show() {

    //产生随机数

      sum=(int)(Math.random());
        switch(sum) {
          case 1:
            action="剪刀";
            break;
          case 2:
            action="石头";
            break;
          case 3:
            action="布";
            break;
      }
             System.out.println(name+"出拳"+action);
          }
      }


    第三个类:

    package Lianxi;

    import java.util.Scanner;

    public class Rjcq02 {

    //游戏类

      Scanner input=new Scanner(System.in);

    //定义游戏类的属性

    //创建玩家类的对象

      renjicaiquan it=new renjicaiquan();

    //创建电脑类的对象

      renjicaiquan1 is=new renjicaiquan1();
        int number;
        int witch=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:
              is.name = "刘备";
              break;
            case 2:
              is.name = "孙权";
              break;
            case 3:
              is.name = "曹操";
              break;

        }
          b = false;
          } else {
              System.out.println("输入数字有误,请重新输入");
        }
          } while (b);
              System.out.print("请输入你的姓名:");
        it.name = input.next();
              System.out.println(it.name + " VS " + is.name + " 对战 ");
              System.out.println("要开始吗?(y/n)");
        char answer = input.next().charAt(0);
        while (answer == 'y') {
          it.show();
          is.show();
          if (it.num == is.sum) {
              System.out.println("和局");
          } else if (((it.num == 2) && (is.sum == 1))|| (it.num == 1) && (is.sum == 3)|| ((it.num == 3) && (is.sum == 2))) {
              System.out.println("你赢了,真棒!");
          it.score++;
        } else {
              System.out.println("你输了,真笨!");
          is.score++;
        }
          witch++;
              System.out.println(" ");
              System.out.println("还要继续吗?(y/n)");
          answer = input.next().charAt(0);
        }
        }
    /**
    * 定义游戏结算的方法
    */
      public void showResult() {
              System.out.println("********************************");
              System.out.println(it.name + " VS " + is.name);
              System.out.println("对战次数:" + witch);
              System.out.println(" 姓名 得分");
              System.out.println(it.name + " " + it.score);
              System.out.println(is.name + " " + is.score);
          if (it.score < is.score) {
              System.out.println("你真笨,下次加油!");
          } else if (it.score == is.score) {
              System.out.println("竟然平局,我们下次一决胜负!");
          } else {
              System.out.println("你真棒!太赞了!!");
        }
              System.out.println("********************************");

          }
        }


    测试类:

    package java12;

    public class renjicaiquan3 {
    public static void main(String[] args) {
      renjicaiquan2 play=new renjicaiquan2();                  //创建游戏类的对象
      play.process();                //调用游戏类的游戏进程方法
      play.showResult();               //调用游戏类的结算方法
      }

  • 相关阅读:
    电话聊天狂人
    PAT 1080. MOOC期终成绩
    PAT 1079. 延迟的回文数
    [转载]matlab 中序列求极值的方法
    [转载][转]Matlab-寻找峰值函数
    [转载]Kirchhoff Migration Impulse Response
    Envelope Extraction Using the Analytic Signal
    [转载]地震动的挑选
    C笔记
    Git常用有用命令
  • 原文地址:https://www.cnblogs.com/szhhhh/p/9790114.html
Copyright © 2011-2022 走看看