Syntax error when asking if a variable equals another variable in batch

拜拜、爱过 提交于 2019-12-02 11:14:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!