我有一个越来越大的批处理文件,使用GTR/ LSS运算符.问题是这些if陈述只允许我输入某些值.在以下示例中,键入2,3或4的值将返回"值太高".
:char_intb
set char_points=30
set /a limit=!char_points!-3
set /p char_int="How many points for intelligence? "
if "!char_int!" GTR "!limit!" echo Value too high. && goto char_intb
if "!char_int!" GTR "!char_points!" echo Insufficient points. && goto char_intb
if "!char_int!" LSS "1" echo Select a value greater than or equal to 1. && goto char_intb
set /a char_points=!char_points!-!char_int!
echo You now have !char_points! remaining.
Run Code Online (Sandbox Code Playgroud)
还有其他人.据我测试过,它似乎只允许我键入0或11.
set /p char_month="Month (0-11): "
if "!char_month!" GTR "11" echo Enter a value between 0 and 11. && echo. && goto last_char
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
不要使用双引号"!char_month!" GTR "11"进行数字比较,在这种情况下cmd会进行字符串比较.这应该工作:
@ECHO OFF &SETLOCAL
:last_char
set /p "char_month=Month (0-11): "
if %char_month% GTR 11 (
echo Enter a value between 0 and 11.
echo.
goto last_char
)
ECHO %char_month%
Run Code Online (Sandbox Code Playgroud)