四、小白学JAVA-石头剪刀布游戏

1、如何从控制台获取用户输入

复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 石头剪刀布的思路
        // 1 2 3 石头 剪刀 布
        Scanner scanner = new Scanner(System.in);
        System.out.println("请出拳:1.石头  2.剪刀 3.布【请输入数字1或2或3】");
        int userInput = Integer.parseInt(scanner.nextLine());
        switch (userInput) {
            case 1:
                System.out.println("您出的是石头");
                break;
            case 2:
                System.out.println("您出的是剪刀");
                break;
            case 3:
                System.out.println("您出的是布");
                break;
        }
    }
}

2、设计电脑如何随机出石头剪刀布

复制代码
int computerResult = (int) (Math.random() * 3 + 1);
复制代码
switch (computerResult) {
    case 1:
        System.out.println("电脑出的是石头");
        System.out.println("游戏结果:您失败了");
        break;
    case 2:
        System.out.println("电脑出的也是剪刀");
        System.out.println("游戏结果:平局");
        break;
    case 3:
        System.out.println("电脑出的是布");
        System.out.println("游戏结果:您赢了");
        break;
}

3、如何实现游戏的多轮玩法

复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 石头剪刀布的思路
        // 1 2 3 石头 剪刀 布
        while (true) {
            System.out.println("新一轮游戏开始**************************************************************************************");
            int computerResult = (int) (Math.random() * 3 + 1);
            // System.out.println(computerResult);
            Scanner scanner = new Scanner(System.in);
            System.out.println("请出拳:1.石头  2.剪刀 3.布【请输入数字1或2或3】  退出游戏请输入q");
            String quitResult = scanner.nextLine();
            if (quitResult.equals("q")) {
                System.out.println("退出游戏成功,欢迎下次再来!");
                break;
            }
            int userInput = Integer.parseInt(quitResult);
            switch (userInput) {
                case 1:
                    System.out.println("您出的是石头");
                    switch (computerResult) {
                        case 1:
                            System.out.println("电脑出的也是石头");
                            System.out.println("游戏结果:平局");
                            break;
                        case 2:
                            System.out.println("电脑出的也是剪刀");
                            System.out.println("游戏结果:您赢了");
                            break;
                        case 3:
                            System.out.println("电脑出的也是布");
                            System.out.println("游戏结果:您失败了");
                            break;
                    }
                    break;
                case 2:
                    System.out.println("您出的是剪刀");
                    switch (computerResult) {
                        case 1:
                            System.out.println("电脑出的是石头");
                            System.out.println("游戏结果:您失败了");
                            break;
                        case 2:
                            System.out.println("电脑出的也是剪刀");
                            System.out.println("游戏结果:平局");
                            break;
                        case 3:
                            System.out.println("电脑出的是布");
                            System.out.println("游戏结果:您赢了");
                            break;
                    }
                    break;
                case 3:
                    System.out.println("您出的是布");
                    switch (computerResult) {
                        case 1:
                            System.out.println("电脑出的是石头");
                            System.out.println("游戏结果:您赢了");
                            break;
                        case 2:
                            System.out.println("电脑出的是剪刀");
                            System.out.println("游戏结果:您失败了");
                            break;
                        case 3:
                            System.out.println("电脑出的也是布");
                            System.out.println("游戏结果:平局");
                            break;
                    }
                    break;
            }
        }
    }
}
相关推荐
sg_knight1 分钟前
Claude Code 如何辅助定位 Bug 和问题代码
java·前端·bug·ai编程·claude·code·claude-code
counting money2 分钟前
HttpServlet基础
java
吴声子夜歌5 分钟前
JavaScript——面向对象
java·开发语言·javascript
钱多多_qdd5 分钟前
第一次使用mac,安装java相关的东西
java·python·macos
阿kun要赚马内9 分钟前
Python五类数据容器的对比和通用方法
开发语言·python
RE-190110 分钟前
Polars:告别 Pandas 性能瓶颈,用 Rust 驱动的 DataFrame 库处理亿级数据
开发语言·rust·pandas·polars·ai生成
波波00712 分钟前
每日一题:请解释.NET 中的泛型约束是什么
java·面试·.net
2301_7938046913 分钟前
C++中的备忘录模式
开发语言·c++·算法
好家伙VCC14 分钟前
# 发散创新:用 Rust 实现高性能事件驱动架构的实践与优化 在现代软件系统中,**事件驱动编程模型**已经成为构
java·开发语言·python·架构·rust