猜隨機(jī)數(shù)小游戲
@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
該游戲?yàn)椤安聰?shù)字”游戲,玩家需要在 5 次機(jī)會(huì)內(nèi)猜出計(jì)算機(jī)隨機(jī)生成的 1 到 100 的整數(shù)。游戲界面使用 CMD 窗口,每次玩家輸入一個(gè)猜測(cè)的數(shù)字后,程序會(huì)判斷該數(shù)字與答案之間的大小關(guān)系,并給出提示,直到玩家猜出了正確的答案或者用盡了所有機(jī)會(huì)。
你可以將上述代碼保存為一個(gè) .bat 文件,然后雙擊運(yùn)行即可在 CMD 界面中開始游戲。
學(xué)習(xí)時(shí)的痛苦是暫時(shí)的 未學(xué)到的痛苦是終生的
本文版權(quán)歸作者和博客園共有,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,歡迎轉(zhuǎn)載 轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明,否則保留追究法律責(zé)任的權(quán)利。

浙公網(wǎng)安備 33010602011771號(hào)