简体   繁体   English

Windows cmd批处理文件计算

[英]windows cmd batch file calculations

The output of the code below just repeats "Unbalanced parentheses"x100. 以下代码的输出仅重复“不平衡括号” x100。 If I take out the parentheses and leave %%i%5 it just loops numbers 1-100 without the mod. 如果我删除括号并留下%% i%5,它将仅循环1-100而没有mod。 If I make it add or subtract, it works fine. 如果我将其添加或减去,它将正常工作。 Why won't it find the mod, specifically? 具体为什么找不到模组呢?

    :MAIN
     setlocal EnableDelayedExpansion
     set result=0
     for /L %%i in (1,1,100) do (
     set /a result= (%%i%5)
     echo !result!
     )
     endlocal

I have this other bit of code with a problem I can't seem to find. 我还有其他代码,但似乎找不到问题。 If nonzero numbers are entered, it works just fine. 如果输入非零数字,则可以正常工作。

@echo off

:MAIN
set /p number1= "Enter 1st number:”
if %number1%== "9" goto second_num

:second_num
set /p number2= “Enter 2nd number:”
if %number2%== “0” goto error
if %number2%== "9" goto division

:division
set /a result= (%number1%/%number2%)
echo %result%
pause
exit

:error
echo "Error. Cannot divide by 0. Start over."
goto MAIN

But if the second number is 0, it's output is: 但是,如果第二个数字为0,则输出为:

Divide by zero error.
ECHO is off.
Press any key to continue...

And when I press a key, the program ends instead of going to :error. 当我按一个键时,程序结束,而不是:error。 I realize there's a problem with an if statement. 我意识到if语句存在问题。 And why does it say ECHO is off? 为什么说ECHO关闭了? Could someone please point me in the right direction? 有人能指出我正确的方向吗?

 set /a result= (%%i%5)

is your problem. 是你的问题。 The closing parenthesis is seen by the FOR as its closing parenthesis, so the SET has unbalanced parentheses. FOR 结束括号视为结束括号,因此SET的括号不平衡。

Change the ) in the SET to ^) - the caret ( ^ ) escapes the ) telling the processor that it's part of the set , not of the for SET)更改为^) -脱字符号( ^ )转义)告诉处理器它是set的一部分,而不是for

While you're at it, the %5 should be %%5 becaue bizarrely, % escapes % , not ^ and the processor needs to be told that the % isn't part of %5 - the fifth parameter to the batchfile. 在使用它时, %5应该是%%5因为奇怪的是, %转义了% ,而不是^并且需要告知处理器该%不是%5一部分-批处理文件的第五个参数。

The next problem is caused by not understanding that an environment variable is ALWAYS a string. 下一个问题是由于不了解环境变量始终为字符串而引起的。 The only time quotes are needed is when it may contain certain characters like SPACES. 唯一需要加引号的时间是当它可能包含某些字符(例如SPACES)时。 The statement if %var%==7 is comparing the string contents of var with the string 7 , hence your statements are comparing the contents of the variable with "9" (or whatever). if %var%==7的语句if %var%==7的字符串contents of var与字符串7进行比较,则您的语句将the contents of the variable"9" (或其他值)进行比较。 These are NEVER going to be equal. 这些绝不会平等。 Now - "%number2%" may be equal, as both of the actual STRINGS may be equal. 现在- "%number2%"可能相等,因为两个实际STRINGS可能相等。

If ECHO has nothing as its "argument" then it reports its state, that is either ON or OFF. 如果ECHO没有任何参数作为其“参数”,则它将报告其状态,即为ON或OFF。 SInce RESULT was not SET because of the error attempting to divide by 0, the statement boils down to ECHO so the echo state is duly reported. 由于由于尝试除以0的错误而未设置RESULT ,因此该语句归结为ECHO因此已适当报告了回声状态。

The cure is to try ECHO. 解决方法是尝试ECHO. - the dot gives ECHO something to chew on and ot'll produce a blank line since %result% is set to [nothing] -由于%result%设置为[nothing],该点使ECHO可以咀嚼一些东西,并且会产生空白行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM