问题
Here is my code:
if %magic%==%weakness% set /a damage=%random%*3/32767+12
if %magic==%resistance% set /a damage=%random%*3/32767+5
echo Your attack does %damage% damage.
I keep getting a syntax error when I run this. What am I doing wrong?
回答1:
You should always use quotes around your variables or use delayed expansion, to avoid problems when one or both variables are empty.
Btw. in the second line you missed one percent
if "%magic%"=="%weakness%" set /a damage=%random%*3/32767+12
if "%magic%"=="%resistance%" set /a damage=%random%*3/32767+5
echo Your attack does %damage% damage.
Or with delayed expansion
setlocal EnableDelayedExpansion
if !magic!==!weakness! set /a damage=%random%*3/32767+12
if !magic!==!resistance! set /a damage=%random%*3/32767+5
echo Your attack does !damage! damage.
来源:https://stackoverflow.com/questions/18536743/syntax-error-when-asking-if-a-variable-equals-another-variable-in-batch