双IF不起作用?

时间:2017-04-07 17:30:18

标签: batch-file if-statement conditional-statements

使用此代码我想说

if %var% > 5 AND if %var% <10 THEN. 

它适用于2,例如它是&#34; no&#34;但对于&#34; 11&#34;该程序结束并关闭,而不是遵循&#34; else&#34;你能帮我解决一下吗?

代码:

@echo off
:DEBUT
set /p "VAR=Type your text:"
if %VAR% geq 5 (
     if %VAR% leq 10 (
echo YEAH
goto DEBUT
  )
) else (
echo NO
goto DEBUT
)

输出:

Type your text:2
NO
Type your text:4
NO
Type your text:5
YEAH
Type your text:6
YEAH
Type your text:8
YEAH
Type your text:10
YEAH
Type your text:

2 个答案:

答案 0 :(得分:1)

这是重写的尝试。

@Echo Off
:DEBUT
Set/P "VAR=Type your text: "
If %VAR% GEq 5 If %VAR% LEq 10 Echo=YEAH
GoTo DEBUT

修改

如果您希望在每种情况下输出YEAH或NO,请参阅以下一种方法:

@Echo Off
:DEBUT
Set "_="
Set/P "VAR=Type your text: "
If %VAR% GEq 5 If %VAR% LEq 10 Set "_=T"
If Not Defined _ (Echo=NO) Else Echo=YEAH
GoTo DEBUT

和另一个:

@Echo Off
:DEBUT
Set/P "VAR=Type your text: "
If %VAR% GEq 5 (If %VAR% LEq 10 (Echo YEAH) Else Echo NO) Else Echo NO
GoTo DEBUT

答案 1 :(得分:0)

只是提供另一种选择:不需要嵌套if s

:DEBUT
set "var=0"
Set/P "VAR=Type your text: "
If %VAR% lss 5 echo NO&goto :DEBUT
If %VAR% gtr 10 echo NO&goto :DEBUT
echo yes
goto :DEBUT

set "var=0"预设变量以捕获空输入(用户只需按RETURN)