开发者

My batch script doesn't work. Can anyone tell me why?

I need help with my script. All of the stuff keeps falling th开发者_如何转开发rough and not doing the goto. Can you help?

@ECHO OFF
goto joshua
:joshua
Echo Hello my name is Joshua.
echo What is yours?
set /p name=
cls
Echo Hello %name%.
cls
Echo How are you?
set /p feeling=
cls
echo Is this good?
echo Im sorry I am still learning human feelings.
set /p humanfeelings=Yes/No
if %humanfeelings%==Yes goto Good
if %humanfeelings%==No goto Bad
cls
:Good
Echo Well that is pleasent.
Echo Would you like to play a game %name%?
set /p answer=Yes/No
if %answer%==Yes goto startmenu
if %answer%== No goto quit
cls
:Bad
echo That is not good.
echo Would you like to play a game %name%?
set /p answera=Yes/No
if %answera%==Yes goto startmenu
if %answera%== No goto later
cls
:startmenu
start menu.bat
cls
:later
Echo That is fine %name%, maybe later
pause
exit


Your original code with line numbers:

1 :  @ECHO OFF
2 :  goto joshua
3 :  :joshua
4 :  Echo Hello my name is Joshua.
5 :  echo What is yours?
6 :  set /p name=
7 :  cls
8 :  Echo Hello %name%.
9 :  cls
10:  Echo How are you?
11:  set /p feeling=
12:  cls
13:  echo Is this good?
14:  echo Im sorry I am still learning human feelings.
15:  set /p humanfeelings=Yes/No
16:  if %humanfeelings%==Yes goto Good
17:  if %humanfeelings%==No goto Bad
18:  cls
19:  :Good
20:  Echo Well that is pleasent.
21:  Echo Would you like to play a game %name%?
22:  set /p answer=Yes/No
23:  if %answer%==Yes goto startmenu
24:  if %answer%== No goto quit
25:  cls
26:  :Bad
27:  echo That is not good.
28:  echo Would you like to play a game %name%?
29:  set /p answera=Yes/No
30:  if %answera%==Yes goto startmenu
31:  if %answera%== No goto later
32:  cls
33:  :startmenu
34:  start menu.bat
35:  cls
36:  :later
37:  Echo That is fine %name%, maybe later
38:  pause
39:  exit
  • Line 2: goto joshua unnecessary since joshua is at the very next line.
  • Line 9: cls erases the preceding echo (hides Hello %name%.)
  • Line 16, 17, 30, and 31: variables should be quoted or else unexpected input (e.g., hell no!!! or yes, i am.) would cause a syntax error
  • Line 18, 25, and 32: there is no default case, so if the answer is neither "Yes" nor "No", the script continues to the next line.

I'd also suggest changing these if statements into if-else, and use case-insensitive string comparison with if /I. Try these modifications:

@echo off

:joshua
echo Hello. My name is Joshua.
echo What is yours?
set /p name=
cls
echo Hello, %name%.
echo How are you?
set /p feeling=
cls

echo Is this good?
echo I'm sorry. I am still learning human feelings.
set /p humanfeelings=Yes/No 

if /I "%humanfeelings%"=="yes" (
    goto Good
) else (
    goto Bad
) 
cls

:Good
echo Well that is pleasant.
goto AskPlay

:Bad
echo That is not good.

:AskPlay
echo Would you like to play a game, %name%?
set /p answera=Yes/No
if /I "%answera%"=="yes" (
    goto startmenu
) else (
    goto later
)
cls

:startmenu
start menu.bat
cls

:later
echo That is fine, %name%. Maybe later!
pause
exit


Okay, from what I see wrong here is that you don't jump anywhere in the end of Good/Bad handling. This means that no matter if you type "Yes" or "No", Bad/startmenu/later blocks are always executed, one after another. Is that the problem you're having? Then just add some more goto-s.


Are you typing the full answer Yes or No in response to the prompts? If you abbreviate to Y and N, only those initial characters will get stored.


Try removing the space before the word No in these lines:

if %answer%== No goto quit

and

if %answera%== No goto later

Like many of us who have outlived our usefulness, the batch interpreter is old and fussy. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜