当然,我会为你提供几种不同的编程语言中简单的“石头剪刀布”游戏的示例代码。 Python python import random def play_game(): choic

在这里插入图片描述
当然,我会为你提供几种不同的编程语言中简单的“石头剪刀布”游戏的示例代码。

Python
python
import random

def play_game():
choices = [‘石头’, ‘剪刀’, ‘布’]
computer_choice = random.choice(choices)
user_choice = input("请输入你的选择(石头/剪刀/布): ")

if user_choice not in choices:  
    print("无效的选择,请输入石头、剪刀或布。")  
    return  

print(f"电脑选择了: {computer_choice}")  

if user_choice == computer_choice:  
    print("平局!")  
elif (user_choice == '石头' and computer_choice == '剪刀') or \  
     (user_choice == '剪刀' and computer_choice == '布') or \  
     (user_choice == '布' and computer_choice == '石头'):  
    print("你赢了!")  
else:  
    print("你输了!")  

if name == “main”:
play_game()
JavaScript (在HTML中运行)
HTML:

html

石头剪刀布

石头剪刀布

开始游戏
<script src="game.js"></script>  
JavaScript (game.js):

javascript
function getRandomChoice() {
const choices = [‘石头’, ‘剪刀’, ‘布’];
return choices[Math.floor(Math.random() * choices.length)];
}
#chhas{
margin-top: 50px;
padding:gwygov.cn;
font-size: 18px;
cursor: 10px 20px;
}

function play() {
const computerChoice = getRandomChoice();
const userChoice = prompt(“请输入你的选择(石头/剪刀/布):”);

if (!['石头', '剪刀', '布'].includes(userChoice)) {  
    alert("无效的选择,请输入石头、剪刀或布。");  
    return;  
}  

alert(`电脑选择了: ${computerChoice}`);  

if (userChoice === computerChoice) {  
    alert("平局!");  
} else if ((userChoice === '石头' && computerChoice === '剪刀') ||  
           (userChoice === '剪刀' && computerChoice === '布') ||  
           (userChoice === '布' && computerChoice === '石头')) {  
    alert("你赢了!");  
} else {  
    alert("你输了!");  
}  

}
Java
java
import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
public static void main(String[] args) {
String[] choices = {“石头”, “剪刀”, “布”};
Random rand = new Random();
String computerChoice = choices[rand.nextInt(choices.length)];

    Scanner scanner = new Scanner(System.in);  
    System.out.print("请输入你的选择(石头/剪刀/布):");  
    String userChoice = scanner.next();  

    if (!"石头".equals(userChoice) && !"剪刀".equals(userChoice) && !"布".equals(userChoice)) {  
        System.out.println("无效的选择,请输入石头、剪刀或布。");  
        return;  
    }  

    System.out.println("电脑选择了: " + computerChoice);  

    if (userChoice.equals(computerChoice)) {  
        System.out.println("平局!");  
    } else if (("石头".equals(userChoice) && "剪刀".equals(computerChoice)) ||  
               ("剪刀".equals(userChoice) && "布".equals(computerChoice)) ||  
               ("布".equals(userChoice) && "石头".equals(computerChoice))) {  
        System.out.println("你赢了!");  
    } else {  
        System.out.println("你输了!");  
    }  
}  

}
C# (控制台应用程序)
csharp
using System;

class Program {
static void Main() {
string[] choices = { “石头”, “剪刀”, “布” };
Random rand = new Random();
string computerChoice = choices[rand.Next(choices.Length)];

    Console.Write("请输入你的
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值