设置/p 空答案崩溃 [英] set /p empty answer crash
问题描述
我是新来的,所以我会尽力做到最好.
i am new here so i'll try to be as good as i can.
所以我正在尝试制作一个基于基于文本的 MS-DOS 的 RPG,而且我做得很好,因为我刚刚看到如果用户在 set/p 处输入无效输入,例如空答案(只需按下输入)或不在IF"上的答案,批处理就会崩溃,我想解决这个问题,这样它就不会那么崩溃了.
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
提前致谢
推荐答案
不是set/p
崩溃了,而是:
if %input%==new
如果 %input% 为空,则解析为:
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"
效果很好.
当变量只包含空格和/或制表符时也是如此:
The same applies when the variable contains only spaces and/or tabs:
if == new
(语法错误)与 if ""== 新"
(运行良好)
if == new
(syntax error) versus if " " == "new"
(running fine)
完整代码如下:
: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
这篇关于设置/p 空答案崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!