设置/ p空答案崩溃

时间:2014-12-15 17:37:59

标签: batch-file crash string

我是新来的,所以我会尽力做到最好。

所以我试图制作一个基于文本的MS-DOS的RPG,我很顺利,因为我刚看到如果用户在set / p输入无效输入,就像一个空答案(只是按下)输入)或答案不在" IF",批次只是崩溃,我想解决这个问题,以免它崩溃。

以下是我想修复的部分之一:

@echo off
title "Wasteland Adventure"
color 0A
cls
:Menu
cls
echo.
echo.
echo Welcome to Wasteland Adventure
echo.
echo To start a new game, type NEW and press ENTER.
echo To see instructions for the game, type INSTRUCTIONS and press ENTER.
echo To quit, type QUIT and press ENTER.
set input=
set /p input=What do you want to do? 
if %input%==new goto INTRO
if %input%==instructions goto INSTRUCTIONS
if %input%==quit goto EXIT

提前致谢

1 个答案:

答案 0 :(得分:2)

它不是崩溃的set /p,而是:

if %input%==new 

如果%input%为空,则将其解析为:

if ==new 

显然是语法错误。为避免这种情况,请使用:

if "%input%"=="new"

然后将空输入解析为:

if ""=="new"

工作正常。完整的代码如下:

:Menu
set input=
set /p input=What do you want to do? 
if "%input%"=="new" goto INTRO
if "%input%"=="instructions" goto INSTRUCTIONS
if "%input%"=="quit" goto EXIT
REM for any other (invalid) input:
goto :Menu