java 石头剪子布游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 石头剪子布游戏相关的知识,希望对你有一定的参考价值。

源代码 StoneGame.java

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 import java.util.Random;
 5 
 6 public class StoneGame {
 7     private static int playerScores = 0;
 8     private static int computerScores = 0;
 9     private static boolean playing = true;
10     private static String[] playerSkills = {"石头", "剪子", "布"};
11 
12     public static void main(String[] args) {
13         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
14         try {
15             System.out.println("输入:石头[1] 剪子[2] 布[3] 退出[4]");
16             while (playing) {
17                 int playerSkill = Integer.parseInt(bufferedReader.readLine());
18                 Random random = new Random();
19                 int computerSkill = random.nextInt(3) + 1;
20                 if (playerSkill == 4) {
21                     System.out.println("Bye.");
22                     playing = false;
23                     break;
24                 }
25                 if (playerSkill > 0 && playerSkill < 4) {
26                     System.out.println("玩家: " + playerSkills[playerSkill - 1]);
27                     System.out.println("电脑: " + playerSkills[computerSkill - 1]);
28                     compare(playerSkill, computerSkill);
29                     System.out.println("玩家分数: " + playerScores + " " + "电脑分数: " + computerScores);
30                 } else {
31                     System.out.println("请重试.");
32                 }
33             }
34         } catch (IOException e) {
35             e.printStackTrace();
36         } catch (NumberFormatException e) {
37             System.out.println("请输入正确的数字!");
38         }
39     }
40 
41     private static void compare(int playerSkill, int computerSkill) {
42         if (playerSkill == computerSkill) {
43             System.out.println("平局.");
44             return;
45         }
46         if (playerSkill == 1) { // player skill is stone.
47             if (computerSkill == 2) {
48                 System.out.println("玩家胜利.");
49                 playerScores++;
50             } else {
51                 System.out.println("电脑胜利.");
52                 computerScores++;
53             }
54         }
55         if (playerSkill == 2) { // player skill is scissor.
56             if (computerSkill == 3) {
57                 System.out.println("玩家胜利.");
58                 playerScores++;
59             } else {
60                 System.out.println("电脑胜利.");
61                 computerScores++;
62             }
63         }
64 
65         if (playerSkill == 3) { // player skill is cloth.
66             if (computerSkill == 1) {
67                 System.out.println("玩家胜利.");
68                 playerScores++;
69             } else {
70                 System.out.println("电脑胜利.");
71                 computerScores++;
72             }
73         }
74     }
75 }

 

以上是关于java 石头剪子布游戏的主要内容,如果未能解决你的问题,请参考以下文章

一本通1132:石头剪子布

python小游戏,石头/剪子/布

Java练习 SDUT-2733_小鑫の日常系列故事——石头剪子布

1-7-04:石头剪子布

团队-游戏石头剪子布-需求分析

石头剪子布的规则