批处理编程=“ set / p”将不接受空格作为答案

时间:2019-01-26 15:18:08

标签: batch-file cmd scripting

我知道我要问的确切问题已经有了答案,但是我试图理解该答案,或者与提出答案的人联系,但我没有走很远。

我在说的是这里

batch "set /p" crashes when no input

但是我不知道如何从那里的答案中修复代码。

以下代码的问题是,当我按1时,它进入1回答,当我按2时,它进入2回答,当我按输入时,它进入代码的开头,没关系,但是,当我键入空格然后按Enter键时,代码崩溃。回答时,请记住,我是个孩子,根本不了解代码,因此我可能不理解复杂的答案。

对不起,如果我拼错了一点,英语不是我的母语。

代码示例:

@echo off
:start
cls
set input=x

set /p input=Enter input:

if %input%==1 goto 1answered
if %input%==2 goto 2answered

echo A mistake was made
pause >NUL
goto start

:1answered
echo You entered the number 1
pause >NUL
exit

:2answered
echo You entered the number 2
pause >NUL
exit

此外,这是我正在处理的代码。 我知道它不好,但是我不知道更好:/

@echo off

(
set /p usernameofplayer=
)<usernameofplayer.txt

set input=x

mode 85,30

color 0b

title LiteTec.inc

goto mainpage

::The code on top of mainpage is just for looks, random.

:mainpage
cls
echo.
echo var dog = ( -0.19 ) ^+ Jon / ( 0.15 ) ^* -foo(y,x,rule(destroy() ^* ROWS,920.47))
echo            /^* terminals used ^*/
echo       var num, id_var, id_func, id_idx, binaryOP, quote, assignOP;
echo this.getRandomRule = function () {
echo                if (this.rules.length == 0) {
echo                     return ""
echo       WELCOME TO
echo       _      ____  ______    ___ ______    ___    __ 
echo      ^| ^|    ^|    ^|^|      ^|  /  _]      ^|  /  _]  /  ]
echo      ^| ^|     ^|  ^| ^|      ^| /  [_^|      ^| /  [_  /  / 
echo      ^| ^|___  ^|  ^| ^|_^|  ^|_^|^|    _]_^|  ^|_^|^|    _]/  /  
echo      ^|     ^| ^|  ^|   ^|  ^|  ^|   [_  ^|  ^|  ^|   [_/   \_ 
echo      ^|     ^| ^|  ^|   ^|  ^|  ^|     ^| ^|  ^|  ^|     ^\     ^|
echo      ^|_____^|^|____^|  ^|__^|  ^|_____^| ^|__^|  ^|_____^|^\____^|
echo.
echo         For help type "help" and click enter
echo.
goto command




:command
set /p input=#%usernameofplayer%^>

if %input%==reset goto mainpage
if %input%==help goto help
if %input%==chat goto chat
if %input%==access goto accessnotspecified
if %input%==x goto command
if %input%==ELSE goto command



:help
echo.
echo ================================================================
echo To use a command, type in its name and click enter
echo To learn sub commands of a command, type the command
echo with [] at the end
echo example: command[]
echo To use a sub command of a command, type the sub command
echo in the [] of the command
echo List of commands available:
echo =reset
echo =chat
echo =access
echo =
echo ================================================================
goto command




:accessnotspecified
echo.
echo What to access is not specified
echo.
goto command

1 个答案:

答案 0 :(得分:1)

我建议将命令部分更改为此:

:command
Set "input="
Set /P "input=#%usernameofplayer%>"

If /I "%input%"=="reset" GoTo mainpage
If /I "%input%"=="help" GoTo help
If /I "%input%"=="chat" GoTo chat
If /I "%input%"=="access" GoTo accessnotspecified
GoTo command

...如您所见,在输入提示双引号时,无需转义 >


作为旁注,您还可以将某些主页部分更改为:

Echo var dog = ( -0.19 ) + Jon / ( 0.15 ) * -foo(y,x,rule(destroy() * ROWS,920.47))
Echo            /* terminals used */

...因为不需要逃避 + *