If statement 为什么批处理if语句不起作用?
我已经有一段时间没有使用这个代码了。请看一看,告诉我出了什么问题,这样我就可以解决它了。此代码将成为xprize竞赛的一部分If statement 为什么批处理if语句不起作用?,if-statement,batch-file,If Statement,Batch File,我已经有一段时间没有使用这个代码了。请看一看,告诉我出了什么问题,这样我就可以解决它了。此代码将成为xprize竞赛的一部分 echo [Math] echo. echo. echo. echo Question 1 echo. echo. echo. echo If Mary goes to the market and buys three echo bananas for two dollars each, how much ech
echo [Math]
echo.
echo.
echo.
echo Question 1
echo.
echo.
echo.
echo If Mary goes to the market and buys three
echo bananas for two dollars each, how much
echo did she spend altogether?
echo.
echo.
echo (A) $3
echo (B) $6
echo (C) $5
echo (D) $1
echo.
echo.
echo.
set /p ANSWER01=Answer:
cls
goto results
:results
set /a "COUNTER=0"
以下是我无法理解的:
我不知道如何更改if语句中变量的值
if %ANSWER01%==b(
set /a "COUNTER=COUNTER+1"
)
echo %COUNTER%
pause
在
b
和(
AAMOI,
if/i
将使匹配不区分大小写。您应将choice命令用于:
Choice /c abcd /m "Answer: "
set ANSWER01=%errorlevel%
同样,对于您的if语句,只需这样做:
if %ANSWER01% equ 2 set /a "COUNTER=COUNTER+1"
那会更容易更好
注意:如果使用选项
命令,则eq 2
是
您好,是否需要启用延迟扩展选项,并在括号中为if语句留出一个空格:
您的代码类似于:
@PeterWrite如果用户没有选择“b”,它会执行“echo%COUNTER”。如果用户没有选择
b
,则您的代码会工作并回显计数器的值(0)。但是,如果/i“%answer01%”==“b”,则使用可能更好(“允许用户只需按
ENTER。如果用户不试图破坏程序,代码将很好地工作-如果您按照我的建议在
b`和
之间添加空格()。嗯,不-不是这种情况。如果变量在“块”内更改,您只需要delayedexpansion
。”(一系列带括号的语句)并且您希望访问更改后的值(当您需要使用!var!
而不是%var%
-!var!
来访问更改后的值,而%var%
用于输入块之前的值)
set /a counter=0
setlocal ENABLEDELAYEDEXPANSION
echo [Math]
echo.
echo.
echo.
echo Question 1
echo.
echo.
echo.
echo If Mary goes to the market and buys three
echo bananas for two dollars each, how much
echo did she spend altogether?
echo.
echo.
echo (A) $3
echo (B) $6
echo (C) $5
echo (D) $1
echo.
echo.
echo.
set /p ANSWER01=Answer:
cls
goto results
:results
if %ANSWER01%==b (
set /a counter=counter+1
)
echo %counter%
pause
endlocal