Java猜拳

package com.sxt;

import java.util.Scanner;

public class caiquan {
    public static void main(String[] args) {
        while (true)
        {
            Scanner sc=new Scanner(System.in);
            System.out.println("======欢迎使用猜拳=======");
            System.out.println("请输入你的选择 0:剪刀 1:石头 2:布");
            int person=sc.nextInt();
            int computer=(int)(Math.random()*3)+1;
            String peo="人类";
            String com="电脑";
            switch (person)
            {
                case 0:
                    peo="剪刀";
                    break;
                case 1:
                    peo="石头";
                    break;
                case 2:
                    peo="布";
                    break;
                default:
            }
            switch (computer)
            {
                case 1:
                    com="剪刀";
                    break;
                case 2:
                    com="石头";
                    break;
                case 3:
                    com="布";
                    break;
                default:
            }
            if (person==0&&computer==3||person==1&&computer==0&&person==2||computer==1)
            {
                System.out.println("你出"+peo+",计算机出"+com+",你赢了");
                break;
            }
            if (person==computer)
            {
                System.out.println("你出"+peo+",计算机出"+com+",平局!请再出拳");
            }
            else
            {
                System.out.println("你出"+peo+",计算机出"+com+",你输了请再出拳");
            }
        }


    }



}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Java 猜拳游戏的示例代码: ```java import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] choices = {"rock", "paper", "scissors"}; int computerChoice = (int) (Math.random() * 3); // 电脑随机出拳 System.out.print("Please choose rock, paper, or scissors: "); String userChoice = scanner.next().toLowerCase(); while (!userChoice.equals("rock") && !userChoice.equals("paper") && !userChoice.equals("scissors")) { System.out.print("Invalid input. Please choose rock, paper, or scissors: "); userChoice = scanner.next().toLowerCase(); } System.out.println("You chose " + userChoice + "."); System.out.println("The computer chose " + choices[computerChoice] + "."); if (userChoice.equals(choices[computerChoice])) { System.out.println("It's a tie!"); } else if ((userChoice.equals("rock") && choices[computerChoice].equals("scissors")) || (userChoice.equals("paper") && choices[computerChoice].equals("rock")) || (userChoice.equals("scissors") && choices[computerChoice].equals("paper"))) { System.out.println("You win!"); } else { System.out.println("You lose!"); } scanner.close(); } } ``` 代码中,首先定义了三个选择项:rock、paper 和 scissors。接着,使用 `Math.random()` 方法随机生成电脑的出拳结果。然后,通过 `Scanner` 类获取用户输入的选择,如果输入值不合法,则提示重新输入。最后,通过比较用户和电脑的出拳结果来判断胜负,并输出相应的结果。 注意,这只是一个简单的示例代码,实际应用中可能需要加入更多的逻辑和交互设计。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值