集/对空回答崩溃 [英] set /p empty answer crash

查看:193
本文介绍了集/对空回答崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,所以我会尽力,因为我可以为好。

i am new here so i'll try to be as good as i can.

所以,我试图做一个RPG基于基于文本的MS-DOS,然后我要去pretty以及我刚才看到,如果用户把一个无效的输入在设定/ P,像一个空的答案(只需pressing进入)或答案是不是如果,批量刚刚崩溃了,我想解决这个问题,因此会少crashy。

So i am trying to make a RPG based on text-based MS-DOS, and i am going pretty well as i just saw that if the user puts an invalid input at set /p, like an empty answer (just pressing enter) or an answer which is not on the "IF", the batch just crashes, and I would like to fix that so it will be less crashy.

下面是其中的一个部分,我想解决:

Here is one of the parts i'd like to fix:

@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

在此先感谢

推荐答案

这不是设置/ P 的崩溃,但是:

if %input%==new 

如果%的输入%是空的,这被分析为:

if %input% is empty, this is parsed as:

if ==new 

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

obviously a syntax error. To avoid this, use:

if "%input%"=="new"

然后空输入将被解析为:

An empty input will then be parsed as:

if ""=="new"

这工作正常。完成code是这样的:

which works fine. Complete code like this:

: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

这篇关于集/对空回答崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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