猜随机数小游戏

@echo off
title Guess the Number Game
color 0a
set /a num=%random% %% 100 + 1
echo I'm thinking of a number between 1 and 100.
echo You have 5 chances to guess it.
set /a guess=0
set /a tries=5
:guessloop
set /p guess=Guess the number:
set /a tries-=1
if %guess%==%num% (
    echo Congratulations! You guessed it!
    pause
    goto end
) else (
    if %tries%==0 (
        echo Sorry, you ran out of tries. The number was %num%.
        pause
        goto end
    ) else (
        if %guess% lss %num% (
            echo The number is higher than your guess. You have %tries% tries left.
        ) else (
            echo The number is lower than your guess. You have %tries% tries left.
        )
        goto guessloop
    )
)
:end
echo Thanks for playing!
pause

该游戏为“猜数字”游戏,玩家需要在 5 次机会内猜出计算机随机生成的 1 到 100 的整数。游戏界面使用 CMD 窗口,每次玩家输入一个猜测的数字后,程序会判断该数字与答案之间的大小关系,并给出提示,直到玩家猜出了正确的答案或者用尽了所有机会。

你可以将上述代码保存为一个 .bat 文件,然后双击运行即可在 CMD 界面中开始游戏。

posted @ 2023-03-18 15:43  卷心菜的奇妙历险  阅读(46)  评论(0编辑  收藏  举报