本文实例为大家分享了Java实现人机猜拳游戏的具体代码,供大家参考,具体内容如下
实现:
User类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class User { private String name; private int score= 0 ; private int num; public String GetName() { return this .name; } public void SetName(String name) { this .name=name; } public int GetScore() { return this .score; } public void SetScore( int score) { this .score+=score; } } |
Computer类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public class Computer { private String name; private int score= 0 ; private int num; public String GetName() { return this .name; } public void SetName(String name) { this .name=name; } public int RandNums() { int n; n=( int )(Math.random()* 3 )+ 1 ; // 返回1到3的随机整数。 return n; } public int GetScore() { return this .score; } public void SetScore( int score) { this .score+=score; } } |
Gamemanager类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
import java.util.Scanner; public class GameManager { public static void main(String[] args) { Scanner input= new Scanner(System.in); //创建一个键盘扫描类对象 User user= new User(); Computer computer= new Computer(); int vsNums= 0 ; System.out.println( "出拳游戏规则:1、剪刀,2、石头,3、布" ); System.out.println( "请选择对方角色(1、刘备,2、孙权,3、曹操)" ); int n=input.nextInt(); //输入整型 switch (n) { case 1 : computer.SetName( "刘备" ); break ; case 2 : computer.SetName( "孙权" ); break ; case 3 : computer.SetName( "曹操" ); break ; } System.out.println( "请输入你的姓名" ); String name=input.next(); //输入字符串型 user.SetName(name); System.out.println(user.GetName()+ " " + "VS" + " " +computer.GetName()); String flag= "y" ; while (flag.equals(flag)) { System.out.println( "要开始吗y/n" ); String yOrn=input.next(); //输入字符串型 if (yOrn.equals( "y" )) { vsNums++; System.out.println( "请出拳:1、剪刀,2、石头,3、布(输入数字)" ); int nums=input.nextInt(); //输入整型 switch (nums) { case 1 : System.out.println( "你出拳:" + "剪刀" ); break ; case 2 : System.out.println( "你出拳:" + "石头" ); break ; case 3 : System.out.println( "你出拳:" + "布" ); break ; } int rand=computer.RandNums(); switch (rand) { case 1 : System.out.println(computer.GetName()+ "出拳:" + "剪刀" ); break ; case 2 : System.out.println(computer.GetName()+ "出拳:" + "石头" ); break ; case 3 : System.out.println(computer.GetName()+ "出拳:" + "布" ); break ; } if (nums== 1 && rand== 3 || nums== 2 && rand== 1 || nums== 3 && rand== 2 ) { System.out.println( "恭喜,你赢了" ); user.SetScore( 1 ); } else if (nums==rand) { System.out.println( "平手了" ); } else { System.out.println( "很遗憾,你输了" ); computer.SetScore( 1 ); } } else { System.out.println(computer.GetName()+ " " + "VS" + " " +user.GetName()); System.out.println( "对战次数:" +vsNums); System.out.println( "姓名\t得分" ); System.out.println(user.GetName()+ "\t" +user.GetScore()); System.out.println(computer.GetName()+ "\t" +computer.GetScore()); if (user.GetScore()>computer.GetScore()) { System.out.println( "恭喜,恭喜" ); } else { System.out.println( "继续加油" ); } break ; } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_44350205/article/details/107600296