【问题标题】:Windows batch label loopWindows 批处理标签循环
【发布时间】:2017-03-25 07:07:20
【问题描述】:

很难运行我的批处理文件。
我相信这与我的 if 语句有关,它不会失败。
我总是以“如果此时出乎意料”结束。
请任何帮助不胜感激

:firstNumber
set /p "firstNum = Enter the first number (other than 22) "
if "%firstNum%"=="22" (
goto :end
) else (
goto :secondNumber
)

:secondNumber
set /p "secondNum = Enter the second number (other than 22 and 0) "
if "%secondNum%"=="22" (
goto :end
)   if "%secondNum%"=="0" (
goto :errorNumber
) else (
goto :command
)

:errorNumber
echo You cannot divide by 0
goto :secondNumber

:command
set /a quotient=%firstNum% / %secondNum% 
echo "%firstNum% divided by %secondNum% = !quotient!

:end

【问题讨论】:

  • 我会在set /P 之前插入一个标准的set 命令,以便在用户简单地按enter 的情况下定义一个默认值,例如set "firstNum=0"set "secondNum=0" , 分别;然后我将删除 if 比较中的引号并将运算符 == 替换为 EQU 以便进行数字比较而不是字符串比较(如 if %secondNum% EQU 0);最后我会缩进出现在一对括号中的所有命令以提高可读性...

标签: loops batch-file if-statement


【解决方案1】:
)   if "%secondNum%"=="0" (

应该是

)
if "%secondNum%"=="0" (

) 终止之前的if 语句。 cmd 认为该行的其余部分不应遵循 )


(稍后 - 缺少操作数...)

批处理对SET 语句中的空格很敏感。 SET FLAG = N 将名为“FLAGSpace”的变量设置为“SpaceN”的值

因此,您的 set /p "firstnum = ... 设置了一个名为“firstNumSpace”的变量 - 这与变量“firstNum”不同

因为firstnumsecondnum 都不包含值,所以cmd 看到

设置 /a quotient= /

在您的 set 语句中去掉 = 两边的空格。

【讨论】:

  • 在我进行更改后,我现在收到的错误是“缺少操作数除以 =”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2013-01-25
  • 1970-01-01
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
相关资源
最近更新 更多