本文實(shí)例為大家分享了Java實(shí)現(xiàn)人機(jī)猜拳游戲的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn):
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的隨機(jī)整數(shù)。 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); //創(chuàng)建一個(gè)鍵盤掃描類對(duì)象 User user= new User(); Computer computer= new Computer(); int vsNums= 0 ; System.out.println( "出拳游戲規(guī)則:1、剪刀,2、石頭,3、布" ); System.out.println( "請(qǐng)選擇對(duì)方角色(1、劉備,2、孫權(quán),3、曹操)" ); int n=input.nextInt(); //輸入整型 switch (n) { case 1 : computer.SetName( "劉備" ); break ; case 2 : computer.SetName( "孫權(quán)" ); break ; case 3 : computer.SetName( "曹操" ); break ; } System.out.println( "請(qǐng)輸入你的姓名" ); 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( "請(qǐng)出拳:1、剪刀,2、石頭,3、布(輸入數(shù)字)" ); 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( "對(duì)戰(zhàn)次數(shù):" +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( "繼續(xù)加油" ); } break ; } } } } |
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/weixin_44350205/article/details/107600296