goto unexpected with blank choice

青春壹個敷衍的年華 提交于 2020-02-05 05:17:07

问题


My problem: I set a VAR with prompt and three IF to let choice. Fourth choice doesn't work. I would like that if i push enter without type anything, it goes back to :MOSTRA. Actually leaving it blank and typing enter on the keyboard it give back GOTO WAS UNEXPECTED and close CMD. Where am i wrong?

:MOSTRA
ECHO Make your choice.
ECHO.
ECHO A) Ok
ECHO B) Ko
ECHO Esci
SET /P choose=Scelta: 
if /I %choose%==A GOTO OK
if /I %choose%==B GOTO KO
if /I %choose%==esci GOTO FINE
if /I %choose%=="" GOTO ERROR


:ERROR
ECHO You type nothing.
ECHO.
GOTO MOSTRA

:KO
ECHO Bad choice
ECHO.
GOTO MOSTRA

:OK
ECHO Right choice
ECHO.
GOTO MOSTRA

:FINE
exit

回答1:


SET /P choose=Scelta: 
if /I "%choose%"=="A" GOTO OK
if /I "%choose%"=="B" GOTO KO
if /I "%choose%"=="esci" GOTO FINE
if /I "%choose%"=="" GOTO ERROR

CMD will substitute the value of choose in the place of %choose% so

if /I %choose%==A GOTO OK 

is interpreted as

if /I ==A GOTO OK

which generates the error.

There is a more subtle error, too.

If you first choose a or b, you get the "right choice" or "bad choice" response, correctly BUT if your next choice is simply ENTER the response doesn't change - the previous response will be repeated.

The reason is that set /p does not change choose if you simply type ENTER. What you need is

SET "choose="
SET /P choose=Scelta: 
if /I "%choose%"=="A" GOTO OK

which sets choose to [nothing]



来源:https://stackoverflow.com/questions/17025303/goto-unexpected-with-blank-choice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!