2011-04-14 49 views
3

我在做用户输入并检查是否键入了ny ...并且因为两种方式都表示无法正常工作。通过批处理文件检查输入

这是我有:

@echo off 
set /P theuserinput="Type your name: " 
echo So your name is: %theuserinput%? 
set /P isit="Y/N: " 
echo You typed: %isit% 
if (%isit% == "y") goto :saidyes 
if (%isit% == "n") goto :saidno 

:saidyes 
echo Hooray! 

:saidno 
echo Aww 
PAUSE 

回答

4

首先,你可以在这两个如果的后添加默认跳转。

然后,在这两个测试中,您必须在%isit%周围添加引号并删除括号。您也可以添加/ I标志来执行不敏感的字符串比较。

最后,在每个回声之后添加goto以跳过下一个回声。

@echo off 
set /P theuserinput="Type your name: " 
echo So your name is: %theuserinput%? 
set /P isit="Y/N: " 
echo You typed: %isit% 
if /I "%isit%" == "Y" goto :saidyes 
if /I "%isit%" == "N" goto :saidno 
goto :error 

:saidyes 
echo Hooray! 
goto :end 

:saidno 
echo Aww 
goto :end 

:error 
echo ERROR 

:end 
PAUSE 
+0

啊...是的.. gotos ...嘿嘿。谢谢!我会在两分钟之内标记出来。谢谢! – nn2 2011-04-14 16:39:15

+0

我复制粘贴上面的脚本,并得到与问题相同的问题。我删除了(),它工作。也许这不应该是被接受的答案。 – 2011-04-15 02:10:46

+0

感谢Vik,在发布之前我还没有测试过我的代码(我目前在Linux上),结果如下...我刚更正了代码。 – cedrou 2011-04-15 08:21:18

2

需要语法

改变这里是修改后的代码

@echo off 
set /P theuserinput="Type your name: " 
echo So your name is: %theuserinput%? 
set /P isit="Y/N: " 
echo You typed: %isit% 
if "%isit%" == "Y" GOTO saidyes 
if "%isit%" == "N" GOTO saidno 

:saidyes 
echo Hooray! 
GOTO paused 

:saidno 
echo Aww 

:paused 
PAUSE 

....

在上述实施例中的Y/N被认为是唯一的大写字母。

+0

+1如果有条件使它对我有效,则删除周围的人。 – 2011-04-14 16:58:23

2

有几件事我会做不同的这个,保证其正常运行......

首先更正后的代码如下,但在底部是我的推荐代码:

@echo off 
setlocal 
set theuserinput= 
set /P theuserinput="Type your name: " 
echo So your name is: %theuserinput%? 
set /P isit="Y/N: " 
echo You typed: %isit% 
if '%isit%'== 'Y' goto saidyes 
if '%isit%'=='N' goto saidno 


:saidyes 
echo Hooray! 
goto end 

:saidno 
echo Aww 
goto end 

:end 
pause 
endlocal 
exit 

然而,以确保你只能得到是或否通过添加有几件事情我会放...

首先,我会确保你只抢到的第一个字母:

IF NOT '%isit%'=='' set isit=%isit:~0,1% 

接下来我将确保你只有大写字母,所以你做掉,如果他们是小写的,因为这实际上不是在某些情况下工作,如果这种情况是不正确的:

if '%isit%'== 'y' set isit=Y 
if '%isit%'== 'Y' goto :saidyes 
if '%isit%'=='n' set isit=N 
if '%isit%'=='N' goto :saidno 

此文件的最后修改可能看起来像以下:

@echo off 
:top 
set theuserinput= 
set /P theuserinput="Type your name: " 
echo So your name is: %theuserinput%? 
set /P isit="Y/N: " 
IF NOT '%isit%'=='' set isit=%isit:~0,1% 
IF '%isit%'=='y' set isit=Y 
IF '%isit%'=='Y' goto saidyes 
IF '%isit%'=='n' set isit=N 
IF '%isit%'=='N' goto saidno 
goto top 

:saidyes 
echo You typed: %isit% 
echo Hooray! 
goto end 

:saidno 
echo You typed: %isit% 
echo Aww 
goto end 

:end 
pause 
exit 
2

报价在DOS批处理文件通常被误用。与UNIX shell脚本不同,DOS批处理语言没有经过深思熟虑。例如,当isit变量包含引号

set isit="Y" 

那么上述if语句扩展到

IF ""Y"" == "Y" GOTO saidyes 
IF ""Y"" == "N" GOTO saidno 

因为cmd.exe的计算表达式之前不会删除"%isit%"=="Y"引号。

isit中的引用不应与在此处发布的原始批处理文件发生。但是,您经常在批处理文件中处理路径名,Windows将长文件名用引号引起来隐藏空白。例如,如“C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC”。要解决这一点,通常的做法是写if语句这样:

IF ["%isit%"] == ["Y"] GOTO saidyes 
IF [%isit%] == ["Y"] GOTO saidyes 
IF [%isit%] == [Y] GOTO saidyes 

对于set isit="Y"变为:

IF [""Y""] == ["Y"] GOTO saidyes 
IF ["Y"] == ["Y"] GOTO saidyes 
IF ["Y"] == [Y] GOTO saidyes 

set isit=Y

IF ["Y"] == ["Y"] GOTO saidyes 
IF [Y] == ["Y"] GOTO saidyes 
IF [Y] == [Y] GOTO saidyes 

set isit=

IF [""] == ["Y"] GOTO saidyes 
IF [] == ["Y"] GOTO saidyes 
IF [] == [Y] GOTO saidyes 

这远不够优雅,但至少现在适用于带引号和不带引号的变量。它也适用于isit为空时。 Oftenly批处理文件停止,因为空的变量:

set x= 
REM ... 
IF %x% == x GOTO x 

因为现在的if语句扩展为:

IF == x GOTO x 

CMD.EXE失败。这些错误通常很难调试。

诡计很古老;我记得在MSDOS中已经使用它。请注意,方括号不会被cmd.exe评估;他们只是一些很少使用的字符。但这只是一个窍门。任何先进的批处理脚本需要一个dequoting功能:

@echo off 
setlocal EnableExtensions 
setlocal EnableDelayedExpansion 
REM ... 
set /P isit="Y/N: " 
call :dequote isit 
REM ... 
IF "!isit!" == "Y" GOTO saidyes 
IF "!isit!" == "N" GOTO saidno 
REM ... 
goto done 

:dequote 
for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A 
goto :eof 

:done 

deqote -subroutine消除周围传递给函数的变量名称的任何双引号。

在引用[]之后,就不再需要了。还请注意使用!而不是%。感叹号强制cmd.exe重新展开isit

0

我改变了一些代码来帮助解决你的问题。而不是goto:(功能)你只是说goto(功能)

@echo off 
set /P theuserinput=Type your name: 
echo So your name is: %theuserinput%? 
set /p isit=Y/N: 
echo You typed: %isit% 
if %isit% == "y" goto saidyes 
if %isit% == "n" goto saidno 

:saidyes 
echo Hooray! 

:saidno 
echo Aww 
pause 
+0

但它没有效果,因为两种变体都是允许的 – jeb 2014-11-09 10:19:46

相关问题