批处理文件“echo”和功能输出不正确

时间:2015-12-24 20:30:17

标签: batch-file

我的“回音”输出有问题。

我有3个功能(Speak,SpeakByChoice,SpeakChoose)和1个标签(Play)

到目前为止,这是我的代码......

@ echo off
title Text Story Game

:Main
color 9f
echo ====== Main Menu ======
echo 1) Play
echo 2) Password
echo 3) Quit
set /p mainMenuChoice=
if %mainMenuChoice% == 1 goto Play
if %mainMenuChoice% == 2 goto Password
if %mainMenuChoice% == 3 exit
goto Main

:Play
REM Gameplay Start
call :Speak "Mayor" "Mmm, oh, I didn't see you there!"
call :Speak "Mayor" "Hello there! Welcome to Rockwell Town, er...who are you?"
set /p name= Choose your name: 
call :Speak "Mayor" "Oh, why hello %name%! Good to meet you."
call :Speak "Mayor" "What did you come here for?"
call :SpeakChoose "I need a favor done." "Um, nothing..." (here is problem)
pause
pause

:Speak
echo.
echo [ %~1 ^>^> %~2 ] 
pause>nul
goto :EOF

:SpeakByChoice
echo.
call :Speak %~1 %~2
call :Speak %~3 %~4
goto :EOF

:SpeakChoose
echo.
echo [ Choice: ]
echo 1) %~1
echo 2) %~2
set /p SpeakChoose=
if %SpeakChoose% == 1 call :SpeakByChoice "%name%" "%~1" "Mayor" "Okay, what can I help you with?" 
if %SpeakChoose% == 2 call :SpeakByChoice "%name%" "%~2" "Mayor" "Oh okay....wait a minute. Didn't you ask me about moving in earlier?"
goto :EOF

:DNCSpeakChoose
echo.
echo You didn't choose anything.....
call :Speak "%name%" "..."

虽然输出是这样的:

我的名字是“(空白)”,但在SpeakByChoice通话中显示为“I”,但在Speak等其他通话中则没有显示

====== Main Menu ======
1) Play
2) Password
3) Quit
1

[ Mayor >> Mmm, oh, I didn't see you there! ] ↓

[ Mayor >> Hello there! Welcome to Rockwell Town, er...who are you? ] ↓
Choose your name: (not chosen...)

[ Mayor >> Oh, why hello (blank name IS here) ! Good to meet you. ] ↓

[ Mayor >> What did you come here for? ] ↓

[ Choice: ]
1) I need a favor done.
2) Um, nothing...
1


[ I (who is I?? this should've been blank) >> need ] ↓

[ Mayor >> Okay ] ↓
Press any key to continue . . .
Press any key to continue . . .

我不确定是什么触发了这个问题。它是SETLOCAL,还是我的:EOF,我不知道。但我的Speak和Spe​​akChoose功能完美无缺,但我的SpeakByChoice却没有。

1 个答案:

答案 0 :(得分:0)

只需添加一些引号即可。这对我很有用。

@echo off
title Text Story Game

:Main
color 9f
echo ====== Main Menu ======
echo 1) Play
echo 2) Password
echo 3) Quit
set /p mainMenuChoice=
if %mainMenuChoice% == 1 goto Play
if %mainMenuChoice% == 2 goto Password
if %mainMenuChoice% == 3 exit
goto Main

:Play
REM Gameplay Start
call :Speak "Mayor" "Mmm, oh, I didn't see you there!"
call :Speak "Mayor" "Hello there! Welcome to Rockwell Town, er...who are you?"
set /p name= Choose your name: 
call :Speak "Mayor" "Oh, why hello %name%! Good to meet you."
call :Speak "Mayor" "What did you come here for?"
call :SpeakChoose "I need a favor done." "Um, nothing..." (here is problem)
pause
pause

:Speak
echo.
echo [ %~1 ^>^> %~2 ] 
pause>nul
goto :EOF

:SpeakByChoice
echo.
call :Speak "%~1" "%~2"
call :Speak "%~3" "%~4"
goto :EOF

:SpeakChoose
echo.
echo [ Choice: ]
echo 1) %~1
echo 2) %~2
set /p SpeakChoose=
if %SpeakChoose% == 1 call :SpeakByChoice "%name%" "%~1" "Mayor" "Okay, what can I help you with?" 
if %SpeakChoose% == 2 call :SpeakByChoice "%name%" "%~2" "Mayor" "Oh okay....wait a minute. Didn't you ask me about moving in earlier?"
goto :EOF

:DNCSpeakChoose
echo.
echo You didn't choose anything.....
call :Speak "%name%" "..."