5

尝试制作测验 show.bat 文件并遇到 If-Set 编码问题:

:10    
echo @: (Press any key to continue)
pause >nul
cls
echo Bonus Question 1:
echo Translate the letters in CAPS.
echo @: Copy the sentence and translate the words in CAPITALS.
echo Il nome di mia ZIA e Gabriella e il nome di mio ZIO e Nick.
echo @: Careful, you only get 3 goes!
echo @: now you try!
set /p BonusAnswer1=">"
set /p BonusAnswer1=">"
if "%BonusAnswer1%"=="Il nome di mia aunty Gabriella e il nome di mio uncle Nick. echo well done! &goto:11
if "%BonusAnswer1%"=="Hint" echo @: Copy the sentence and translate ZIA and ZIO. Only put a capital for Il, Gabriella and Nick. &goto:10

这是我遇到问题的地方:

if "%Bonus1Tries%"=="3" set Bonus1Tries=2 &echo you only have %Bonus1Tries% left!

该编码中唯一不起作用的部分是它说:

you only have 3 tries left!

代替

you only have 2 tries left!

意思就是

设置 Bonus1Tries=2

不工作。你能帮忙吗,提前谢谢你!-批处理人

4

2 回答 2

3

在您需要的代码块内delayed expansion,或者您拆分命令(如果可能):

if "%Bonus1Tries%"=="3" set "Bonus1Tries=2"
if "%Bonus1Tries%"=="2" echo you only have %Bonus1Tries% left!
于 2013-07-27T07:46:49.000 回答
0

您必须启用延迟扩展

SETLOCAL ENABLEDELAYEDEXPANSION
set VAR=before
set VAR=after & echo immediate:%VAR%, delayed:!VAR!
ENDLOCAL

正如Raymond Chen所解释的那样

于 2013-07-27T07:40:02.990 回答