batch-file - windows批处理中的双if语句
问题描述
我将从代码开始:
@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),它就会崩溃。
我收到错误:命令的语法不正确
我猜这是一个愚蠢的错误,如果你能指出它,那就完美了。
解决方案
不要关闭回声!它在编写批处理代码时隐藏了重要的细节!
关闭echo NOT后,您将立即看到错误:
该命令的语法不正确。
if "!option!" == "1"(
.
感叹句仅在延迟扩展打开时才有效。Batch 不知道如何在!var!
没有启用它的情况下做什么。
另外..一定要按照@Compo上面的建议去做choice.exe
。它会让你成为一个更好的人,人们在看到你的代码时不会抽搐。我们等了很长时间才添加它。请使用它。
推荐阅读
- php - 此代码不断返回错误:“字段列表”中的未知列“NAME_OF_HOUSE_CHURCH”
- javascript - 我想使用 javascript 数组过滤电子邮件
- javascript - 无法使用 fetch 发送 POST 数据:React
- android - 如何使用 SimpleExoPlayerView 从视频中获取音轨
- angular - 如何将 CCAvenue 与 .Net Core API 和 Angular 5 应用程序集成?
- android - GBoard 键盘 GIF 贴纸集成
- android - 在intellij idea中查找类的导入包
- c# - 如何在 C# 中将 PDF 转换为图像?
- python - python mpi 密钥错误“duplicate_keysharedFilename[0]”
- python - 输出信息到模板(从下一个数组)