2014-01-11 30 views
0

儘管批處理文件現在顯示了適當的損壞編號,但無論如何它只顯示0。它似乎只能生成0作爲它的編號。使批處理文件顯示0以外的數字

另外,第二個問題,如果我能解決這個問題,在這裏,我應該如何把代碼You have Lost!You have Won取決於你是否健康/怪物的生命值低於0

這裏要說的是新的代碼:

@echo off 
set phealth=20 
set shealth=50 
set pdamage=0 
set sdamage=0 
:start 
cls 
echo =================== 
echo Your HP:  %phealth% 
echo Monster HP: %shealth% 
echo =================== 
echo Choices: 
echo =================== 
echo [1] Strike at the slime 
echo [2] Back away 
set /p choice=You choose option: 
if '%choice%'=='1' goto :choice1 
if '%choice%'=='2' goto :choice2 
goto start 

:choice1 
cls 
set /a pdamage=(%random% %% 5) + 1 
set /a phealth=%phealth%-%pdamage% 
set /a sdamage=(%random% %% 8) + 1 
set /a shealth=%shealth%-%sdamage% 
echo =================== 
echo Your HP:  %phealth% 
echo Monster HP: %shealth% 
echo =================== 
echo You strike at the slime. 
echo The slime strikes you back. 
echo You took %pdamage% damage. 
echo The slime took %sdamage% damage. 
PAUSE 
cls 
goto start 

:choice2 
cls 
set /a pdamage=(%random% %% 1) + 1 
set /a phealth=%phealth%-%pdamage% 
echo =================== 
echo Your HP:  %phealth% 
echo Monster HP: %shealth% 
echo =================== 
echo You back away. 
echo The slime strikes at you. 
echo You took %pdamage% damage. 
echo The slime took 0 damage. 
PAUSE 
cls 
goto start 

:end 

回答

0

(%隨機%%% 1)將始終返回0

cls後直接將以下:start

if %shealth% lss 0 echo You won&pause&goto :eof 
if %phealth% lss 0 echo You lost&pause&goto :eof 

另外:

如果你正在進入一個set/p一個字符串,那麼就沒有說,輸入的數據不包含空間。通過這種方式是到"enclose the strings on both sides of the comparison operator in quotes" - 也就是雙引號'not single quotes'

+0

謝謝你的!不幸的是,隨機似乎產生0,即使它是類似 (%random%%24000)+ 1 而且,當文件執行時,獲勝/失敗位會導致語法錯誤...不是爲什麼看到我確實複製了它們。 – user3184932

+1

你在某處並沒有使用'random'作爲變量名,是嗎? (使用'set random'檢查) – Stephan