1

这是我的代码:

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.

运行此程序时,我不断收到语法错误。我究竟做错了什么?

4

1 回答 1

2

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.
于 2013-08-30T15:52:39.607 回答