利用Java实现 :石头剪刀布小游戏(入门可了解)

效果图

由于实现起来非常的简单,初学者可以看看。

这里直接上代码:

首先是RandomNumber.java类,用于产生随机数。即机器玩家的输出:1:石头,2:剪刀,3:布。

package com.cx;

import java.util.Random;

public class RandomNumber {

	public static void main(String[] args) {
		// 使用工具类:
		Random random = new Random();
		int i =1;
		
		while (i < 100) {
			int num = random.nextInt(3)+1;
			System.out.println(num);
			i++;
		}
		
	}

}

接下来是Mygame.java类,游戏规则以及相关事宜

package com.cx;

import java.util.Random;
import java.util.Scanner;

//石头剪刀布

public class Mygame {

	public static void main(String[] args) {
		//实例化键盘扫描对象
		Scanner input = new Scanner(System.in);
		//游戏变量
		String playHuman;	//游戏玩家
		String playRobot;	//机器玩家
		
		int scoreHuman = 0;
		int scoreRobot = 0;
		
		int totalCount = 0;
		
		String loop = "y";
		String msg = "";
		
		//创建伪随机数生成器对象
		Random random = new Random();
		
	//游戏初始化
		System.out.println("****石头剪刀布游戏规则****");
		System.out.println("石头 vs 剪刀       石头赢");
		System.out.println("石头 vs 布           布赢");
		System.out.println("布      vs 剪刀      剪刀赢");
		System.out.println("**************");
		//玩家昵称设置
		System.out.println("请输入你的游戏昵称:");
		playHuman = input.next();
		System.out.println("请输入机器玩家的昵称:");
		playRobot = input.next();

		System.out.println("对战双方:"+playHuman+" VS"+playRobot);
		System.out.println("开始游戏!");
		
		//输出游戏循环菜单
		while(!"n".equals(loop)) {
			totalCount++;
			System.out.println("====石头剪刀布游戏====");
			//人类玩家出拳
			System.out.println("请出拳:(1.石头  2.剪刀  3.布)");
			String boxHuman = input.next();
			//机器玩家出拳 随机数
			String boxRobot = (random.nextInt(3)+1)+"";
			//当局对战胜负的判定
			switch(boxHuman) {
			case "1"://人类玩家出石头
				switch (boxRobot) {
				case "1":
					msg = "双方都出石头,平局";
					break;
				case "2":
					msg = playHuman+"出[石头],"+playRobot+"出[剪刀],人类玩家赢";
					scoreHuman++;
					break;
				case "3":
					msg = playHuman+"出[石头],"+playRobot+"出[布],机器玩家赢";
					scoreRobot++;
					break;
				}
				break;
			case "2"://人类玩家出剪刀
				switch (boxRobot) {
				case "1":
					msg=playHuman+"出[剪刀],"+playRobot+"出[石头],机器玩家赢";
					scoreRobot++;
					break;
				case "2":
					msg = "双方都出剪刀,平局";
					break;
				case "3":
					msg=playHuman+"出[石头],"+playRobot+"出[布],人类玩家赢";
					scoreHuman++;
					break;
				}
				break;
			case "3"://人类玩家出布
				switch (boxRobot) {
				case "1":
					msg=playHuman+"出[布],"+playRobot+"出[石头],人类玩家赢";
					scoreHuman++;
					break;
				case "2":
					msg=playHuman+"出[布],"+playRobot+"出[剪刀],机器玩家赢";
					scoreRobot++;
					break;
				case "3":
					msg = "双方都出布,平局";
					break;
				}
				break;
			}
			System.out.println("对战局数;"+totalCount+"局结果:");
			System.out.println(msg);
			System.out.println("是否继续游戏?()y/n");
			loop = input.next();
			System.out.println();
		}//循环结束
		System.out.println("---对战结束,结果公布---");
		System.out.println("对战双方"+playHuman+" VS "+playRobot);
		System.out.println("对战局数"+totalCount);
		System.out.println(playHuman+"得分:"+scoreHuman);
		System.out.println(playRobot+"得分:"+scoreRobot);
		System.out.println("平局次数:"+(totalCount-scoreHuman-scoreRobot));		
		System.out.println(scoreHuman>scoreRobot?"恭喜人类玩家取得胜利!":(scoreHuman<scoreRobot)?"恭喜机器玩家获得胜利!":"平局!");
		
	}

}

运行,Over

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值