码迷,mamicode.com
首页 > 其他好文 > 详细

人机猜拳

时间:2017-08-14 14:20:46      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:规则   ice   exti   code   输入   匿名   加油   bool   print   

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("\n请出拳: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("\n\n");
81         System.out.println("\t\t***************");
82         System.out.println("\t\t**c猜拳,开始**");
83         System.out.println("\t\t***************");
84         System.out.println("\n\n");
85         System.out.println("出拳规则:1.剪刀\t2.石头\t3.布");
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 + "对战\n");
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("和局,真衰!嘿嘿,等着瞧吧!\n");
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("恭喜,你赢了!\n");
27                 } else {
28                     System.out.println("^_^,你输了,真笨!\n");
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 }

 

人机猜拳

标签:规则   ice   exti   code   输入   匿名   加油   bool   print   

原文地址:http://www.cnblogs.com/fl72/p/7357340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!