zoukankan      html  css  js  c++  java
  • 第十三章:人机猜拳

    人机猜拳:

    public class Computer {

           String name="电脑"; 

           int score=0;

         public int showFist(){ 

              int show=(int)(Math.random()*10)%3+1;                      

              switch(show){ 

                case 1: 

                     System.out.println(name+"出拳:剪刀");                   

                break; 

                case 2: 

                     System.out.println(name+"出拳:石头");    

                break; 

                case 3: 

                     System.out.println(name+"出拳:布");    

                break; 

        } 

             return show;

        } 

    import java.util.*; 

        /**

          *

          * @author student 

          *

          */

    public class Person {

           String name="匿名";

           int score=0; 

       public int showFist(){ 

           System.out.println("请猜拳(1-剪刀,2-石头,    3-布(请选择:))"); 

       Scanner in=new Scanner(System.in); 

           int show=in.nextInt(); 

           switch(show){ 

            case 1: 

                System.out.println(name+"出拳:剪刀");

            break; 

            case 2:

                System.out.println(name+"出拳:石头"); 

            break; 

            case 3:

                System.out.println(name+"出拳:布"); 

            break; 

           } 

           return show; 

       }

    import java.util.*; 

       /**

         *  

         * @author 

         * 

         */

    public class Game { 

          Person person;

         Computer computer;

         int count;          //局数 

       public void initial(){ 

       person=new Person(); 

       computer=new Computer();

       count=0; 

       } 

        public void startGame(){ 

          System.out.println("*欢迎进入人机猜拳*");     

          System.out.println("***************");  

          System.out.println("****猜拳开始****");   

          System.out.println("***************");  

          System.out.println("出拳规则:1.剪刀,2.石头,3.布"); 

       Scanner in=new Scanner(System.in); 

       System.out.print("请选择对方角色:(1.刘备,2.孙权,3.曹操)"); 

       int computerName=in.nextInt(); 

      System.out.print("请输入您的名字");   

      person.name=in.next();

            switch(computerName){ 

              case 1: 

                  computer.name="刘备"; 

              break;

              case 2: 

                  computer.name="孙权"; 

              break;

              case 3: 

                  computer.name="曹操";

              break;      

    }

    System.out.println(computer.name+"VS"+person.     name+" ");

    System.out.println("是否开始游戏(y/n)?"); 

       String con=in.next(); 

       while(con.equals("y")){

                int perShow=person.showFist();

                int comShow=computer.showFist();             

                if(perShow==comShow){ 

                   System.out.println("平局,再加把劲   

                        吧!");    

                  count++; 

       }else if(

              perShow==1&&comShow==2||    

              perShow==2&&comShow==3||   

              perShow==3&&comShow==1

          ){ 

            System.out.println("笨哦!你输了!");     

              computer.score++; 

              count++; 

         }else if(

           perShow==2&&comShow==1||    

           perShow==3&&comShow==2||    

           perShow==1&&comShow==3){

       System.out.println("恭喜!你赢了!");    

        person.score++; 

        count++; 

    System.out.println("是否开始下一轮?y/n"); 

       con = in.next(); 

    System.out.println(computer.name + " VS " +    

          person.name); 

    System.out.println("对战次数:" + count); 

    if (computer.score > person.score){

         System.out.println("结果:*-*,你输了啊,嘿  

           嘿!!");

    } else if (

          computer.score == person.score) {

       System.out.println("结果:*……*,唉!平局啊,好,下次再一决高下!!拜拜"); 

    } else { 

         System.out.println("结果:居然让你小子赢了!!再来再来!!");

         }

      } 

    public class Test { 

    /** 

      * @param args 

      */

    public static void main(String[] args) { 

           Game game=new Game(); 

           game.initial(); 

           game.startGame(); 

      }

    }

  • 相关阅读:
    入门学习
    SQL语句
    Jmeter录制badboy
    post方式提交数据
    get向服务器提交数据
    手机端解析json
    生成JSON字符串
    新闻客户端
    从网络下载的图片加缓存功能
    消息队列机制messageQuene
  • 原文地址:https://www.cnblogs.com/wang2386033566/p/7358373.html
Copyright © 2011-2022 走看看