四、小白学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;
            }
        }
    }
}
相关推荐
山居秋暝LS4 分钟前
【无标题】RTX00安装paddle OCR,win11不能装最新的,也不能用GPU
开发语言·r语言
卢锡荣8 分钟前
单芯通吃,盲插标杆 —— 乐得瑞 LDR6020,Type‑C 全场景互联 “智慧芯”
c语言·开发语言·计算机外设
Xin_ye1008613 分钟前
C# 零基础到精通教程 - 第七章:面向对象编程(入门)——类与对象
开发语言·c#
辰海Coding40 分钟前
MiniSpring框架学习-完成的 IoC 容器
java·spring boot·学习·架构
AI科技星43 分钟前
《数学公理体系·第三部·数术几何》(2026 年版)
c语言·开发语言·线性代数·算法·矩阵·量子计算·agi
小小编程路1 小时前
C++ 多线程与并发
java·jvm·c++
审判长烧鸡1 小时前
【Go工具】go-playground是什么组织?官方的?
开发语言·安全·go
AI视觉网奇1 小时前
linux 检索库 判断库是否支持
java·linux·服务器
kkeeper~1 小时前
0基础C语言积跬步之字符函数与字符串函数(上)
c语言·开发语言
她的男孩1 小时前
从零搭一个企业后台,为什么我把能力拆成 Starter 和 Plugin
java·后端·架构