JavaBase开发一个猜拳游戏(石头剪刀布)

每次用户可以玩10次, 游戏结束之后,要告诉用户得分情况(赢了加3分 平了得1分 输了减1分)

以下是实施代码↓↓↓↓

                             

        //随机
		Random ran = new Random();
		Scanner sc = new Scanner(System.in);
		int fs = 0;//统计分数
		for(int i=1;i<=10;i++) {
			//电脑随机
			int sj = ran.nextInt(3)+1;
			//玩家输入选择
			System.out.println("1、石头\n2、剪刀\n3、布\n请选择:");
			//接收玩家选择
			int wj = sc.nextInt();
			//判断电脑的三种可能
			if(sj==1) {//电脑出的石头
				//判断玩家的选择
				if(wj==1) {//玩家选择石头
					System.out.println("平局玩家得1分");
					fs+=1;
				}else if(wj==2) {//玩家选择剪刀
					System.out.println("电脑胜利,玩家减1分");
					fs-=1;
				}else {//玩家选择布
					System.out.println("玩家胜利,玩家得3分");
					fs+=3;
				}
				
			}else if(sj==2) {//电脑出的剪刀
				//判断玩家的选择
				if(wj==1) {//玩家选择石头
					System.out.println("玩家胜利,玩家得3分");
					fs+=3;
				}else if(wj==2) {//玩家选择剪刀
					System.out.println("平局,玩家得1分");
					fs+=1;
				}else {//玩家选择布
					System.out.println("电脑胜利,玩家扣1分");
					fs-=1;
				}
			}else if(sj==3) {//电脑出的布
				//判断玩家的选择
				if(wj==1) {//玩家选择石头
					System.out.println("电脑胜利,玩家扣1分");
					fs-=1;
				}else if(wj==2) {//玩家选择剪刀
					System.out.println("玩家胜利,玩家得3分");
					fs+=3;
					
				}else {//玩家选择布
					System.out.println("平局,玩家得1分");
					fs+=1;
				}
			}
		}
		System.out.println("玩家最后得分:"+fs);
    

请给我点个赞再走吧  亲爱的↓↓↓↓

谢谢您啦❥(^_-)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的剪刀石头游戏Java代码: ```java import java.util.Random; import java.util.Scanner; public class RockPaperScissorsGame { public static void main(String[] args) { System.out.println("欢迎来到剪刀石头游戏!"); System.out.println("请输入您的选择:1.剪刀 2.石头 3."); Scanner scanner = new Scanner(System.in); int playerChoice = scanner.nextInt(); if (playerChoice < 1 || playerChoice > 3) { System.out.println("输入无效!"); return; } String[] choices = {"剪刀", "石头", ""}; String computerChoice = choices[new Random().nextInt(choices.length)]; System.out.println("电脑出了:" + computerChoice); int result = getResult(playerChoice, computerChoice); if (result == 0) { System.out.println("平局!"); } else if (result == 1) { System.out.println("恭喜您,您赢了!"); } else { System.out.println("很抱歉,您输了!"); } } public static int getResult(int playerChoice, String computerChoice) { if (playerChoice == 1) { // 玩家出剪刀 if (computerChoice.equals("剪刀")) { return 0; } else if (computerChoice.equals("石头")) { return -1; } else { return 1; } } else if (playerChoice == 2) { // 玩家出石头 if (computerChoice.equals("剪刀")) { return 1; } else if (computerChoice.equals("石头")) { return 0; } else { return -1; } } else { // 玩家出 if (computerChoice.equals("剪刀")) { return -1; } else if (computerChoice.equals("石头")) { return 1; } else { return 0; } } } } ``` 在这个代码中,我们使用了 `Scanner` 类获取用户输入的选择,并且使用 `Random` 类生成电脑的选择。然后,我们使用 `getResult` 函数计算出游戏结果,并且根据结果输出相应的信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值