2011-04-14 45 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

相關問題