批处理脚本不能使If语句有效

时间:2016-11-17 16:02:34

标签: batch-file if-statement statements

我的批处理脚本工作正常,直到我尝试添加一些long if语句。 我是新手,如果有人能查看什么是错误的话。我试图根据你的等级做一个定价计算器。

以下是没有工作的部分。

if "%drating%" < "1500" (set /a price=%price%+3)
else if "%drating%" < "2000" (@set /a price=%price%+5)
else if "%drating%" < "2500" (@set /a price=%price%+6)
else if "%drating%" < "2700" (@set /a price=%price%+8)
else if "%drating%" < "3000" (@set /a price=%price%+10)
else if "%drating%" < "3300" (@set /a price=%price%+12)
else if "%drating%" < "3500" (@set /a price=%price%+14)
else if "%drating%" < "3800" (@set /a price=%price%+20)
else if "%drating%" < "3900" (@set /a price=%price%+30)
else if "%drating%" < "4000" (@set /a price=%price%+40)
else if "%drating%" < "4100" (@set /a price=%price%+50)
else (
echo There is no available price for %drating%.
echo Press any key to exit.
set /p exitkey=
)
继续我在magoo之后所做的事情

if "%drating%" LSS "1500" (set /a price=%price%+3
) else (if "%drating%" LSS "2000" (set /a price=%price%+5
 ) else (if "%drating%" LSS "2500" (set /a price=%price%+6
 ) else (if "%drating%" LSS "2700" (set /a price=%price%+8
 ) else (if "%drating%" LSS "3000" (set /a price=%price%+10
 ) else (if "%drating%" LSS "3300" (set /a price=%price%+12
 ) else (if "%drating%" LSS "3500" (set /a price=%price%+14
 ) else (if "%drating%" LSS "3800" (set /a price=%price%+20
 ) else (if "%drating%" LSS "3900" (set /a price=%price%+30
 ) else (if "%drating%" LSS "4000" (set /a price=%price%+40
 ) else (if "%drating%" LSS "4100" (set /a price=%price%+50
 ) else (echo There is no available price for %drating%.
echo Press any key to exit.
set /p exitkey=
exit
)

2 个答案:

答案 0 :(得分:1)

if %drating% lss 1500 (set /a price=%price%+3
) else (if %drating% lss 2000 (set /a price=%price%+5
 ) else (
 )
)

lss运算符意味着&#34; less&#34;。您需要省略引号,以便执行 numeric 比较,并使用引号执行文字比较。

else子句必须在appropritae括号之前和之后,如同一物理行所示。

必须完成所有括号对。

如果您执行了@echo off语句,则echo命令将被禁止。将此语句放在批处理的开头是正常的。不需要进一步@statement - @只会抑制已解决的以下语句的回声。

答案 1 :(得分:1)

我会简化你的代码,就这样做。

@echo off
set "price=10"
set "drating=4099"
if %drating% lss 1500 (set /a "price+=3" &GOTO SKIP)
if %drating% lss 2000 (set /a "price+=5" &GOTO SKIP)
if %drating% lss 2500 (set /a "price+=6" &GOTO SKIP)
if %drating% lss 2700 (set /a "price+=8" &GOTO SKIP)
if %drating% lss 3000 (set /a "price+=10" &GOTO SKIP)
if %drating% lss 3300 (set /a "price+=12" &GOTO SKIP)
if %drating% lss 3500 (set /a "price+=14" &GOTO SKIP)
if %drating% lss 3800 (set /a "price+=20" &GOTO SKIP)
if %drating% lss 3900 (set /a "price+=30" &GOTO SKIP)
if %drating% lss 4000 (set /a "price+=40" &GOTO SKIP)
if %drating% lss 4100 (set /a "price+=50" &GOTO SKIP)

echo There is no available price for %drating%.
pause
GOTO :EOF

:SKIP
echo drating=%drating% price=%price%

pause