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

    1.选取对战角色

      选择猜拳的对手

    2.猜拳

      开始对战,用户选择出拳,与对手进行比较,提示胜负信息

    3.记录分数

      每局猜拳结束,获胜一方加1分(平局都不加分),结束游戏时,显示对战次数及对战最终结果

    创建类方法

     1 import java.util.Scanner;
     2 
     3 public class Cai {
     4     int se1;
     5     int se2;
     6     String choice1;
     7     String choice2;
     8     String choiceMan;
     9     boolean a1;
    10     boolean a2;
    11     int random = (int) (Math.random() * 3);
    12 
    13     public void A() {
    14         Scanner input = new Scanner(System.in);
    15         do {
    16             System.out.print("
    请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
    17             se1 = input.nextInt();
    18             a1 = false;
    19             switch (se1) {
    20             case 1:
    21                 choice1 = "剪子";
    22                 break;
    23             case 2:
    24                 choice1 = "石头";
    25                 break;
    26             case 3:
    27                 choice1 = "布";
    28                 break;
    29             default:
    30                 System.out.println("请做出正确选择!");
    31                 a1 = true;
    32                 break;
    33             }
    34         } while (a1 == true);
    35     }
    36 
    37     public void B() {
    38         if (random == 0.0) {
    39             random = 3;
    40         }
    41         switch (random) {
    42         case 1:
    43             choice2 = "剪子";
    44             break;
    45         case 2:
    46             choice2 = "石头";
    47             break;
    48         case 3:
    49             choice2 = "布";
    50             break;
    51         }
    52     }
    53 
    54     public void C() {
    55         Scanner input = new Scanner(System.in);
    56         do {
    57             System.out.print("请选择对方的角色(1.刘备 2.孙权 3.曹操):");
    58             se2 = input.nextInt();
    59             a2 = false;
    60             switch (se2) {
    61             case 1:
    62                 choiceMan = "刘备";
    63                 break;
    64             case 2:
    65                 choiceMan = "孙权";
    66                 break;
    67             case 3:
    68                 choiceMan = "曹操";
    69                 break;
    70             default:
    71                 System.out.println("请做出正确选择!");
    72                 a2 = true;
    73                 break;
    74             }
    75         } while (a2 == true);
    76     }
    77 
    78     public void D() {
    79         System.out.println("--------------------欢迎进入游戏世界------------------");
    80         System.out.println("
    
    ");
    81         System.out.println("		***************");
    82         System.out.println("		**c猜拳,开始**");
    83         System.out.println("		***************");
    84         System.out.println("
    
    ");
    85         System.out.println("出拳规则:1.剪刀	2.石头	3.布");
    86     }
    87 }

    使用main方法执行

     1 import java.util.Scanner;
     2 
     3 public class Cai01 {
     4     public static void main(String[] args) {
     5         Cai cen = new Cai();
     6         Scanner input = new Scanner(System.in);
     7         String choice = null;
     8         int bot = 0;
     9         cen.D();
    10         cen.C();
    11         System.out.println("你选择了" + cen.choiceMan + "对战
    ");
    12         System.out.print("要开始吗?(开始按y)");
    13         do {
    14             choice = input.next();
    15             if (choice.equals("y")) {
    16                 cen.A();
    17                 System.out.println("你出拳:" + cen.choice1);
    18                 cen.B();
    19                 System.out.println(cen.choiceMan + "出拳:" + cen.choice2);
    20                 System.out.print("结果说:");
    21                 if (cen.se1 == cen.random) {
    22                     System.out.println("和局,真衰!嘿嘿,等着瞧吧!
    ");
    23                 } else if ((cen.se1 == 1 && cen.random == 3)
    24                         || (cen.se1 == 2 && cen.random == 1)
    25                         || (cen.se1 == 3 && cen.random == 2)) {
    26                     System.out.println("恭喜,你赢了!
    ");
    27                 } else {
    28                     System.out.println("^_^,你输了,真笨!
    ");
    29                 }
    30                 System.out.print("是否开始下一轮?(开始按y):");
    31                 bot++;
    32             } else {
    33                 break;
    34             }
    35         } while (choice.equals("y"));
    36         System.out.println("-------------------------");
    37         System.out.println(cen.choiceMan + " VS " + "匿名");
    38         System.out.println("对战次数:" + bot);
    39         System.out.println("结果:呵呵,笨笨,下次加油啊!");
    40         System.out.println("-------------------------");
    41     }
    42 }
  • 相关阅读:
    谷歌插件开发(实现CSDN快速登陆)
    uva216 c++回溯法
    基于Linux的owncloud搭建
    Android常用代码集合
    详谈 php定时器
    Codeforces 278C Learning Languages(并查集)
    WPF中的快捷键(累积中)
    学从难出学,用从易处用
    面向对象的一种简单理解(贴标签)
    WPF与Winform中的不同(1)
  • 原文地址:https://www.cnblogs.com/fl72/p/7357340.html
Copyright © 2011-2022 走看看