2015-11-16 144 views
1

我有一个批处理文件,要求用户输入一个特定的数字。如果该号码位于给定的一组号码之间,则转到该标签。然而,说用户把100它右转:smallsip在批处理中使用多个GTR,LEQ等“if”语句

总而言之,我想这样做,如果用户在特定范围内键入一个数字(IE 30-99)它会转到特定标签。有什么建议么?

:getadrink 
cls 
echo How many sips will Jackie Chan drink? 

set /p numberofsips=Type Number of Sips Here: 

if %numberofsips% LSS 0 goto waitwhat 
if %numberofsips% GEQ 1 goto smallsip 
if %numberofsips% GEQ 10 goto plenty 
if %numberofsips% GEQ 30 goto toomuch 
if %numberofsips% GEQ 100 goto waytoomuch 

:waitwhat 
cls 
echo what 
pause 
:smallsip 
cls 
echo small sips 
pause 
:plenty 
cls 
echo plenty 
pause 
:toomuch 
cls 
echo too much! 
pause 
:waytoomuch 
cls 
echo WAY TOO MUCH 
pause 

P.S.我一直在这里潜藏无数帖子,获得我用Batch创建的东西的帮助。是的,我知道批次已经过时,但我似乎很喜欢,因为我在2个月前发现了它。

回答

1

代码中的if语句正常工作,但您的逻辑错误。例如,当您输入一个数字50时,条件%numberofsips% GEQ 1已满足,因此以下if语句将永远不会到达。要解决这个问题,只需改变它们的顺序。

另一个问题是,你陷入你不想执行的代码中。例如,如果部分:smallsip已完成(并且您确认了pause),则无意中将继续执行:plenty。为了避免这种情况,您需要使用goto跳到别的地方或exit /B离开批处理脚本。

这里是一个固定的代码:

:getadrink 
cls 
echo How many sips will Jackie Chan drink? 

:askforsips 
set numberofsips=0 
set /p numberofsips=Type Number of Sips Here: 

if %numberofsips% GEQ 100 goto waytoomuch 
if %numberofsips% GEQ 30 goto toomuch 
if %numberofsips% GEQ 10 goto plenty 
if %numberofsips% GEQ 1 goto smallsip 
goto waitwhat 

:waitwhat 
cls 
echo what? 
pause 
goto askforsips 
:smallsip 
cls 
echo small sips 
pause 
exit /B 
:plenty 
cls 
echo plenty 
pause 
exit /B 
:toomuch 
cls 
echo too much! 
pause 
goto askforsips 
:waytoomuch 
cls 
echo WAY TOO MUCH 
pause 
goto askforsips 

这些是我改变的东西:

  • if查询被反转的顺序;
  • if %numberofsips% LSS 0查询被删除,所以如果输入的值为0或更小,则执行:waitwhat;在你的代码中,:waitwhat也被执行,以防值为零,因为没有一个条件得到满足;最后(孤独的)goto waitwhat在这里不是必需的,但更明显的是发生了什么;
  • 引入新标签:askforsips以允许在给出无效值(零或更小)的情况下进行另一用户输入;
  • 变量numberofsips现在在用户提示之前被重置,因为set /P保持前一个值,如果用户刚刚按ENTER;
  • :waitwhat降至:waytoomuch的每个部分都明确终止,或者由goto askforsipsexit /B;
+1

非常感谢!当我回家时我会测试它。这是一个我叫做“成龙上学”的批处理游戏,我不会详细说明为什么,但是它来自成龙看到饮水机的一部分,喝得太多会让他生病。我知道这很愚蠢,但你明白了。再次,我会在我回家的时候测试它。 :) –

+0

它的工作原理!谢谢! :) –