虽然批处理文件现在显示损坏的正确编号,但无论如何都只显示0。它似乎只生成0作为其数字。
另外,第二个问题,如果我可以解决这个问题,我应该在何处以及如何根据您的健康状况/怪物的健康状况是否低于0来放置代码You have Lost!
或You have Won
。
这是新代码:
@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 :(得分:0)
(%random%%% 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
,则不会说输入的数据不包含 Spaces 。克服这个问题的方法是"enclose the strings on both sides of the comparison operator in quotes"
- 即双引号'not single quotes'