十二章 练习

来源:互联网 发布:知乎发展史 编辑:程序博客网 时间:2024/06/10 17:24
public class TestGame  {      public static void main(String[] args)      {          Game game=new Game();          game.satrtGame();      }  }  package ch12;    import java.util.Random;    public class Computer  {        public String name;      public int score;        public int showFist()      {            Random r = new Random();          int num = r.nextInt(3) + 1;          switch (num)          {          case 1:              System.out.println(name+"出的是剪刀");              break;          case 2:              System.out.println(name+"出的是石头");              break;          case 3:              System.out.println(name+"出的是布");              break;          }          return num;      }    }  public class Person  {      public String name;      public int score;      /**      * 用户出拳      *       * @return 返回用户出拳是什么 1 .剪刀 2.石头 3.布      */      public int showFirst()      {          Scanner input = new Scanner(System.in);          int ch = 0;          boolean b = true;          while (b)          {              System.out.println("1.剪刀\t2.石头\t3.布");              ch = input.nextInt();              switch (ch)              {              case 1:                  System.out.println("您出的是剪刀");                  b = false;                  break;              case 2:                  System.out.println("您出的是石头");                  b = false;                  break;              case 3:                  System.out.println("您出的是布");                  b = false;                  break;              default:                  System.out.println("输入错误,请输入1--3 任意整数");                  b = true;                  break;              }            }            return ch;      }    }  public class Game  {      private Person person;// 人玩家      private Computer computer;// 电脑玩家      private int count; // 对战次数      Scanner input = new Scanner(System.in);        private void init()      {          person = new Person();          computer = new Computer();      }        public void satrtGame()      {          init();          int p = 0;// 代表用户出拳          int c = 0;// 代表电脑出拳            System.out.println("************************");          System.out.println("******* 猜拳    开始*******");          System.out.println("************************");          System.out.println("出拳规则1.剪刀2.石头3.布");          System.out.println("请选择对方角色(1.刘备2.孙权3.曹操)");          switch (input.nextInt())          {          case 1:              computer.name = "刘备";              break;          case 2:              computer.name = "孙权";              break;          case 3:              computer.name = "曹操";              break;          }          System.out.println("请输入您的姓名");          person.name = input.next();          System.out.println(person.name + "VS" + computer.name + "对战");          System.out.println("要开始吗y/n");          char a = input.next().charAt(0);          if (a == 'y')          {              char b = 'y';              while (b == 'y')              {                  System.out.print("请出拳");                  p = person.showFirst();                  c = computer.showFist();                  if (p == c)                  {                      System.out.println("和局");                      // 人赢                  } else if (p == 1 && c == 3 || p == 2 && c == 1 || p == 3 && c == 2)                  {                      System.out.println("恭喜你赢了");                      person.score += 1;// person.score=person.score+1;                  } else                  {                      System.out.println("你输了");                      computer.score += 1;                  }                  count++;                  System.out.println("是否开始下一轮y/n");                  b = input.next().charAt(0);              }              showResult();// 显示对战结果                                        } else          {              System.out.println("游戏退出");          }      }      // 显示对战结果      public void showResult(){                    System.out.println(person.name+"VS"+computer.name);          System.out.println("对战次数"+count);          System.out.println();                    System.out.println("姓名\t得分");          System.out.println(person.name+"\t"+person.score);          System.out.println(computer.name+"\t"+computer.score);          if(person.score>computer.score){              System.out.println("你真厉害赢了");                        }else if(person.score<computer.score){              System.out.println("你真笨,输了");          }else{              System.out.println("平局");                        }      }    }  

0 0
原创粉丝点击