我将从代码开始:
@echo off
set number=0
echo What would you like to add to %number%
echo 1. Add nothing
echo 2. Add 1
echo 3. Add number of your choosing
set /p test=
:: doesn't add any number
if "%test%" == "1" (
echo You have choosen to not add anything
pause
goto end
)
::Adds number 1 to 0
if "%test%" == "2" (
set /a number=number+1
echo You have added 1 to the number
goto end
)
::Gives choice to add 2 or 3 to number 0
if "%test%" == "3" (
echo Which number would you like to add
echo 1. Number 2
echo 2. Number 3
set /p option=
::Adds 2 to 0
if "!option!" == "1"(
echo You have added 2 to the number
set /a number=number+2
pause
)
::Adds 3 to 0
if "!option!" == "2"(
echo You have added 3 to the number
set /a number=number+3
pause
)
goto end
)
:end
echo Your number is %number%
pause
这将数字声明为 0,然后给你一个选项,将 0、1、2 或 3 添加到数字 0。我已经这样做了,所以我可以测试在 if 语句中是否有一个 If 语句,该语句会询问对于输入,但我迷路了。
将 0 或 1 添加到数字可以正常工作,但是一旦您选择选项 3(想要添加 2 或 3),它就会崩溃。
我收到错误:命令的语法不正确
我猜这是一个愚蠢的错误,如果你能指出它,那就完美了。
不要关闭回声!它在编写批处理代码时隐藏了重要的细节!
没有关闭回声,你会立即看到错误:
命令的语法不正确。 if "!option!" == "1"(
.
感叹号语法仅在启用延迟扩展时才有效。如果!var!
不启用批处理,则不知道如何处理。
另外..一定要按照@Compo 上面的建议去做choice.exe
。它会让你成为一个更好的人,人们在看到你的代码时不会抽搐。我们等了很长时间才添加它。请使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句