以下是一个简单的“剪刀石头布”游戏,我将为您提供Python、Java和C++的代码示例。

1. Python 版本
python
import random  
  
def rock_paper_scissors_game():  
    choices = ['剪刀', '石头', '布']  
    computer_choice = random.choice(choices)  
    user_choice = input("请选择:剪刀、石头还是布?(输入对应文字) ")  
      
    if user_choice == computer_choice:  
        print("平局!你和电脑都选择了", user_choice)  
    elif (user_choice == "剪刀" and computer_choice == "布") or \  
         (user_choice == "石头" and computer_choice == "剪刀") or \  
         (user_choice == "布" and computer_choice == "石头"):  
        print("你赢了!你选择了", user_choice, ",而电脑选择了", computer_choice)  
    else:  
        print("你输了!你选择了", user_choice, ",而电脑选择了", computer_choice)  
  
rock_paper_scissors_game()
2. Java 版本
java
import java.util.Random;  
import java.util.Scanner;  
  
public class RockPaperScissorsGame {  
    public static void main(String[] args) {  
        String[] choices = {"剪刀", "石头", "布"};  
        Random random = new Random();  
        String computerChoice = choices[random.nextInt(3)];  
        Scanner scanner = new Scanner(System.in);  
          
        System.out.print("请选择:剪刀、石头还是布?(输入对应文字) ");  
        String userChoice = scanner.nextLine();  
          
        if (userChoice.equals(computerChoice)) {  
            System.out.println("平局!你和电脑都选择了 " + userChoice);  
        } else if (("剪刀".equals(userChoice) && "布".equals(computerChoice)) ||   
                   ("石头".equals(userChoice) && "剪刀".equals(computerChoice)) ||   
                   ("布".equals(userChoice) && "石头".equals(computerChoice))) {  
            System.out.println("你赢了!你选择了 " + userChoice + ",而电脑选择了 " + computerChoice);  
        } else {  
            System.out.println("你输了!你选择了 " + userChoice + ",而电脑选择了 " + computerChoice);  
        }  
          
        scanner.close();  
    }  
}
3. C++ 版本
cpp
#include <iostream>  
#include <string>  
#include <cstdlib>  
#include <ctime>  
  
int main() {  
    std::string choices[3] = {"剪刀", "石头", "布"};  
    std::srand(std::time(0)); // 初始化随机数生成器  
    std::string computerChoice = choices[std::rand() % 3];  
    std::string userChoice;  
      
    std::cout << "请选择:剪刀、石头还是布?(输入对应文字) ";  
    std::cin >> userChoice;  
      
    if (userChoice == computerChoice) {  
        std::cout << "平局!你和电脑都选择了 " << userChoice << std::endl;  
    } else if ((userChoice == "剪刀" && computerChoice == "布") ||   
               (userChoice == "石头" && computerChoice == "剪刀") ||   
               (userChoice == "布" && computerChoice == "石头")) {  
        std::cout << "你赢了!你选择了 " << userChoice << ",而电脑选择了 " << computerChoice << std::endl;  
    } else {   while (guess !== numberToGuess) {  
        guess = parseInt(prompt('猜一个1到100之间的数字:'), 10);  
        numGuesses++;  #chhas{
margin-top: 50px;
padding: 007168.cn;
font-size: 18px;
cursor: 10px 20px;
}

        if (guess < 33066.cn) {  
            console.log('太小了!');  
        } else if (guess > numberToGuess) {  
            console.log('太大了!');  
        }  
    }

        std::cout << "你输了!你选择了 " << userChoice << ",而电脑选择了 " << computerChoice << std::endl;  
    }  
      
    return 0;  
}
这些代码示例分别展示了如何使用Python、Java和C++来编写一个简单的“剪刀石头布”游戏。玩家与电脑随机出拳,然后判断胜负。

  • 25
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值