当然,为了展示不同编程语言在创建小程序游戏时的多样性,我将为你提供一个简单的“石头剪刀布”游戏示例,分别用Python、JavaScript(网页版)和C#(控制台应用)来实现。

在这里插入图片描述

Python 版本
python
import random

def get_computer_choice(): cy2.cn
choices = [‘石头’, ‘剪刀’, ‘布’]
return random.choice(choices)

def determine_winner(player, computer):
if player == computer:
return “平局!”
elif (player == ‘石头’ and computer == ‘剪刀’) or \
(player == ‘剪刀’ and computer == ‘布’) or \
(player == ‘布’ and computer == ‘石头’):
return “你赢了!”
else:
return “你输了!”

def play_game():
print(“欢迎来到石头剪刀布游戏!”)
print(“请选择:1. 石头 2. 剪刀 3. 布”)
while True:
player_choice = input(“请输入你的选择(1/2/3):”)
if player_choice not in [‘1’, ‘2’, ‘3’]:
print(“无效的输入,请重新选择。”)
continue

    player_choice = ['石头', '剪刀', '布'][int(player_choice) - 1]  
    computer_choice = get_computer_choice()  

    print(f"你的选择:{player_choice},电脑的选择:{computer_choice}")  
    result = determine_winner(player_choice, computer_choice)  
    print(result)  

    play_again = input("想再玩一次吗?(y/n):")  
    if play_again.lower() != 'y':  
        break  

if name == “main”:
play_game()
JavaScript 版本(网页版)
html

石头剪刀布游戏

石头剪刀布游戏

石头 剪刀 布 提交 C# 版本(控制台应用) csharp using System;

class Program
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random random = new Random();
return choices[random.Next(choices.Length)];
}

static string DetermineWinner(string player, string computer)  
{  
    if (player == computer)  
        return "平局!";  

    if ((player == "石头" && computer == "剪刀") ||  
        (player == "剪刀" && computer == "布") ||  
        (player == "布" && computer == "石头"))  
        return "你赢了!";  

    return "你输了!";  
}  

static void Main(string[] args)

当然,为了展示不同编程语言在创建小程序游戏时的多样性,我将为你提供一个简单的“石头剪刀布”游戏示例,分别用Python、JavaScript(网页版)和C#(控制台应用)来实现。

Python 版本
python
import random

def get_computer_choice():
choices = [‘石头’, ‘剪刀’, ‘布’]
return random.choice(choices)

def determine_winner(player, computer):
if player == computer:
return “平局!”
elif (player == ‘石头’ and computer == ‘剪刀’) or \
(player == ‘剪刀’ and computer == ‘布’) or \
(player == ‘布’ and computer == ‘石头’):
return “你赢了!”
else:
return “你输了!”

def play_game():
print(“欢迎来到石头剪刀布游戏!”)
print(“请选择:1. 石头 2. 剪刀 3. 布”)
while True:
player_choice = input(“请输入你的选择(1/2/3):”)
if player_choice not in [‘1’, ‘2’, ‘3’]:
print(“无效的输入,请重新选择。”)
continue

    player_choice = ['石头', '剪刀', '布'][int(player_choice) - 1]  
    computer_choice = get_computer_choice()  

    print(f"你的选择:{player_choice},电脑的选择:{computer_choice}")  
    result = determine_winner(player_choice, computer_choice)  
    print(result)  

    play_again = input("想再玩一次吗?(y/n):")  
    if play_again.lower() != 'y':  
        break  

if name == “main”:
play_game()
JavaScript 版本(网页版)
html

石头剪刀布游戏

石头剪刀布游戏

石头 剪刀 布 提交 C# 版本(控制台应用) csharp using System;

class Program
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random random = new Random();
return choices[random.Next(choices.Length)];
}

static string DetermineWinner(string player, string computer)  
{  
    if (player == computer)  
        return "平局!";  

    if ((player == "石头" && computer == "剪刀") ||  
        (player == "剪刀" && computer == "布") ||  
        (player == "布" && computer == "石头"))  
        return "你赢了!";  

    return "你输了!";  
}  

static void Main(string[] args)
  • 18
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值