猜拳

 1 /**
 2  * 猜拳
 3  * 
 4  * @author 王启文
 5  *
 6  */
 7 public class FingerGuessing {
 8     private int gesture;
 9     private String hint;
10     private int count;
11 
12     public FingerGuessing(int ea, String hint, int count) {
13         this.gesture = ea;
14         this.hint = hint;
15         this.count = count;
16     }
17 
18     public FingerGuessing() {
19         this.reset();
20     }
21 
22     public void reset() {
23         gesture = (int) (Math.random() * 3 + 1);
24         count = 0;
25     }
26 
27     public String getHint() {
28         return hint;
29     }
30 
31     public boolean judge(int yourgesture) {
32         ++count;
33         if (yourgesture == gesture) {
34             hint = "继续...";
35             return true;
36         } else {
37             if (yourgesture != 3) {
38                 if (yourgesture < gesture) {
39                     hint = "你赢了!!总共划了" + count + "次";
40                     return false;
41                 } else {
42                     hint = "你输了!!一共划了" + count + "次";
43                     return false;
44                 }
45             } else {
46                 if (gesture == 1) {
47                     hint = "你赢了!!一共划了" + count + "次";
48                     return false;
49                 } else {
50                     hint = "你输了!!总共划了" + count + "次";
51                     return false;
52                 }
53             }
54 
55         }
56     }
57 }
 1 import java.util.Scanner;
 2 
 3 /**
 4  * 猜拳(附)
 5  * 
 6  * @author 王启文
 7  *
 8  */
 9 
10 public class Test1 {
11     public static void main(String[] args) {
12         System.out.println("输入的数字:1--石头");
13         System.out.println("输入的数字:2--剪刀");
14         System.out.println("输入的数字:3--布");
15         FingerGuessing fg = new FingerGuessing();
16         Scanner sc = new Scanner(System.in);
17         boolean Correct = true;
18         do {
19             System.out.print("请输入一个正确的数字: ");
20             int yourgesture = sc.nextInt();
21             Correct = fg.judge(yourgesture);
22             System.out.println(fg.getHint());
23         } while (Correct);
24 
25         sc.close();
26     }
27 }

 

posted @ 2014-10-23 21:03  Wqw道  阅读(220)  评论(0)    收藏  举报