批处理脚本自行关闭

时间:2016-01-02 13:32:19

标签: batch-file cmd crash

所以,我正在尝试学习批处理,我遇到了一个问题。我正在进行一个基本的文本冒险来学习IF语句等等,每当我尝试输入第一个命令时,我的代码都会崩溃(没有错误说明)(ROOM01标签中的输入。)这是开始 - >开始第二个标签。

@echo off
Title =The Temple=
:menu
set progress=0
color 5
echo ===The Temple===
echo
echo *By Dexter Gard, 2015
echo
echo *Public Domain
echo
echo Press any key
pause
echo 
:main
echo ======The======
echo -----TEMPLE----
goto ROOM01
:ROOM01
echo you are in a dark room.
echo there is a DOOR to the EAST.
set/p input=Command?
if %input%==GO EAST goto ROOM02
if %input%==EXAMINE ROOM echo you can't see anything. goto ROOM01
if %input%==EXAMINE DOOR echo A large heavy wooden door. goto ROOM01
:ROOM02

2 个答案:

答案 0 :(得分:2)

set/p "input=Command?"
if /i "%input%"=="GO EAST" goto ROOM02

批处理将"quoted string containing separators like spaces"视为单个字符串。我已经为if语句显示的结构是强制性的,使用可能包含空格的字符串。 /i开关使比较不区分大小写。

如果您输入的字符串为set/p,则说明输入的数据并不包含 Spaces 。克服这个问题的方法是"enclose the strings on both sides of the comparison operator in quotes" - 即双引号'not single quotes'

语法SET "var=value"(其中value可以为空)用于确保任何杂散尾随空格不包含在分配的值中。 set /a可以安全地使用"无报"。

答案 1 :(得分:1)

您必须在代码中和输入命令时使用引号,因为它们包含空格,或者您可以将GO EAST更改为GO_EAST,然后您不需要引号。 if子句还需要一些修改才能工作。

:ROOM01
echo you are in a dark room.
echo there is a DOOR to the EAST.
set/p input=Command?
if %input%=="GO EAST" ( 
  goto ROOM02
)
if %input%=="EXAMINE ROOM" ( 
  echo you can't see anything. 
  goto ROOM01
)
if %input%=="EXAMINE DOOR" ( 
  echo A large heavy wooden door. 
  goto ROOM01
)
:ROOM02