本文介绍了goto意外,空白选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:
我设置了一个带提示和三个IF的VAR让选择。
第四选择无效。我想如果我按Enter键而不输入任何内容,它将返回到:MOSTRA。
实际上将其保留为空白,然后在键盘上键入回车,它会退回GOTO,并关闭CMD。
我在哪里错了?

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


推荐答案

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将替换 choose 的值代替%choose%这样

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

if /I %choose%==A GOTO OK

被解释为

if /I ==A GOTO OK

其中会产生错误。

还有一个更细微的错误。

There is a more subtle error, too.

如果您首先选择 a b ,您将获得正确选择或错误选择响应,但如果您的下一个选择只是 ENTER 响应不会改变-先前的响应将重复。

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.

原因是 set / p 不如果您只键入 ENTER ,请更改选择。您需要的是

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

其中设置选择为[无]

这篇关于goto意外,空白选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:44