为什么IF语句导致批处理文件退出? [英] Why do the IF statements cause batch file to exit?

查看:181
本文介绍了为什么IF语句导致批处理文件退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使代码选择1-5的随机数,然后在游戏中选择一个随机区域.问题在于,它每次执行时都会不断退出.它也会忽略%place%变量之间的x.

I have tried to make the code pick a random number 1-5 and it would then choose a random area in the game. The problem is that it keeps exiting whenever it executes. It ignores also the x between the %place% variable.

pause
set place=%random% %% 5
timeout 1
if x%place%x==1 goto cave
if x%place%x==2 goto plains
if x%place%x==3 goto swamp
if x%place%x==4 goto volcano
if x%place%x==5 goto fail
:cave
echo You went for a crusade and ended up in a cave.
pause
:plains
echo You went for a crusade and found a plains biome.
pause
:swamp
echo You went for a crusade and ventured into a swamp.
pause
:volcano
echo You went for a crusade and somehow ended up on a volcano.
pause
:fail
echo Your boat sank and you survived. You lost 5 coins.
pause

推荐答案

您的set语句需要使用/a开关,因为您要进行数学运算.您还需要在结果中添加一个,因为现在您将获得一个0-4范围内的随机数,并且if语句的编号为1-5.我也知道您说过忽略x,但是如果您想让程序正常工作,则需要删除它们.这是程序的编辑副本:

Your set statement needs the /a switch because you are trying to do math. Also you need to add one to the result, because right now you are getting a random number in the range of 0-4, and your if statements are numbered 1-5. Also I know you said to ignore the x's, but they need to go if you want your program to work. Here is an edited copy of your program:

set /a place = %RANDOM% %% 5 + 1
timeout 1
if %place% equ 1 goto cave
if %place% equ 2 goto plains
if %place% equ 3 goto swamp
if %place% equ 4 goto volcano
if %place% equ 5 goto fail
:cave
echo You went for a crusade and ended up in a cave.
pause
:plains
echo You went for a crusade and found a plains biome.
pause
:swamp
echo You went for a crusade and ventured into a swamp.
pause
:volcano
echo You went for a crusade and somehow ended up on a volcano.
pause
:fail
echo Your boat sank and you survived. You lost 5 coins.
pause

这篇关于为什么IF语句导致批处理文件退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆