Repository navigation
Expand file tree
/
Copy pathRockPaperScissors.java
More file actions
61 lines (49 loc) · 2.05 KB
/
Copy pathRockPaperScissors.java
File metadata and controls
61 lines (49 loc) · 2.05 KB
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
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[] choices = {"石头", "剪刀", "布"};
System.out.println("=== 石头剪刀布游戏 ===");
System.out.println("输入 1=石头, 2=剪刀, 3=布, 0=退出");
int wins = 0, losses = 0, draws = 0;
while (true) {
System.out.print("\n请出拳: ");
String input = scanner.nextLine().trim();
if (input.equals("0")) {
break;
}
int player;
try {
player = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("无效输入,请输入 1、2、3 或 0");
continue;
}
if (player < 1 || player > 3) {
System.out.println("无效输入,请输入 1、2、3 或 0");
continue;
}
int computer = random.nextInt(3) + 1;
System.out.println("你出: " + choices[player - 1]);
System.out.println("电脑出: " + choices[computer - 1]);
// 结果判定: 1石头 2剪刀 3布, 循环克制关系 石头>剪刀>布>石头
// 玩家胜当 (player - computer + 3) % 3 == 2
int result = (player - computer + 3) % 3;
if (result == 0) {
System.out.println("结果: 平局!");
draws++;
} else if (result == 2) {
System.out.println("结果: 你赢了!");
wins++;
} else {
System.out.println("结果: 你输了!");
losses++;
}
System.out.printf("当前战绩: %d 胜 / %d 负 / %d 平%n", wins, losses, draws);
}
System.out.println("\n游戏结束!最终战绩: " + wins + " 胜 / " + losses + " 负 / " + draws + " 平");
scanner.close();
}
}