四、小白学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;
            }
        }
    }
}
相关推荐
JosieBook4 分钟前
【SpringBoot】37 核心功能 - 高级特性- Spring Boot 中的 自定义 Starter 完整教程
java·spring boot·后端
guygg8811 分钟前
Alpha稳定分布概率密度函数的MATLAB实现
开发语言·matlab
小二·23 分钟前
Elasticsearch 面试题精编(26题|含答案|分类整理)
java·大数据·elasticsearch
2501_9400940225 分钟前
索尼PSP游戏资源下载 推荐中文汉化ios格式合集分享开源掌机模拟器都支持
游戏·ios·cocoa
BD_Marathon32 分钟前
在 Linux 环境中配置 Eclipse 以开发 Hadoop 应用
java·hadoop·eclipse
草莓熊Lotso1 小时前
C++ 二叉搜索树(BST)完全指南:从概念原理、核心操作到底层实现
java·运维·开发语言·c++·人工智能·经验分享·c++进阶
oliveira-time1 小时前
单例模式中的饿汉式
java·开发语言
凌波粒1 小时前
SpringMVC基础教程(1)--MVC/DispathcerServlet
java·spring·mvc
武子康1 小时前
Java-173 Neo4j + Spring Boot 实战:从 Driver 到 Repository 的整合与踩坑
java·数据库·spring boot·后端·spring·nosql·neo4j
凌波粒1 小时前
SpringMVC基础教程(2)--Controller/RestFul风格/JSON/数据转发和重定向
java·后端·spring·json·restful