java后端基础--石头剪刀布小游戏

用的是idea开发工具,看下效果图:在这里插入图片描述
代码如下:

import java.util.Scanner;

public class Game1ShiTou2 {
    public static void main(String[] args) {
        //让玩家选择游戏
        int a;
        System.out.println("猜数字,请输入\"1\"");
        System.out.println("石头剪刀布,请输入\"2\"");
        System.out.println("老虎棒鸡虫,请输入\"3\"");
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt();
        if (a == 1) {
            game1();
        } else if (a == 2) {
            game2();
        } else if (a == 3) {
            game3();
        } else {
            System.out.println("很抱歉,其他游戏暂未发布!");
        }
    }

    static void game1() {
        System.out.println("你选择的是:猜数字");
        System.out.println("周八开放!");
    }

    static void game2() {
        System.out.println("你选择的是:石头剪刀布");
        //打印列表
        System.out.println("代号\t\t含义");
        System.out.println("—————————————");
        System.out.println("1\t\t剪刀");
        System.out.println("2\t\t石头");
        System.out.println("3\t\t布");
        Scanner sc = new Scanner(System.in);
        System.out.print("请你输入数字:");
        int num = sc.nextInt();
        //电脑随机生成数字1~9,将9个数字用3个数字(1、2、3)代替
        int random, machine;
        random = (int) (Math.random() * 10);
        if (random == 1 || random == 2 || random == 3) {
            machine = 1;
        } else if (random == 4 || random == 5 || random == 6) {
            machine = 2;
        } else {
            machine = 3;
        }
        /*
        	分析游戏结果规律:1剪刀,2石头,3布

电脑:	        1			2			3
用户输入:	1	2	3	1	2	3	1	2	3
差值:       	0	-1	-2	1	0	-1	2	1	0
输赢判断:	P	T	F	F	P	T	T	F	P
结论:	T:-1/2  F:-2/1  P:0

         */
        //判断,用户与计算机猜拳,1剪刀,2石头,3布,machine为电脑,num为玩家输入
        int value=machine-num;//电脑与用户输入差值
        switch (value) {
            case -1:
            case 2:
                System.out.println("恭喜你赢了");
                break;
            case -2:
            case 1:
                System.out.println("很遗憾你输了");
                break;
            case 0:
                System.out.println("平局");
                break;
        }
    }

    static void game3() {
        System.out.println("你选择的是:老虎棒鸡虫");
        System.out.println("周八开放!");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小天博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值