iT邦幫忙

0

為甚麼會出錯?再次不能理解(BATCH)

救救我!/images/emoticon/emoticon02.gif

@ECHO off
echo ------------------  
echo 1. Shutdown(S)
echo 2. File(F)
echo 3. Compile Program(C)
echo ------------------
set /p answer = Choose:
echo %answer%
if %answer% == "S" (
    call shutdown.bat
) else (
    echo unok
)

結果

------------------
1. Shutdown(S)
2. File(F)
3. Compile Program(C)
------------------
Choose:S
0
unok

請問為什麼是讀到0?/images/emoticon/emoticon06.gif

ccutmis iT邦高手 4 級 ‧ 2020-11-24 20:53:28 檢舉
@ECHO off
echo ------------------
echo 1. Shutdown(S)
echo 2. File(F)
echo 3. Compile Program(C)
echo ------------------
SET /P answer=Choose:
echo "%answer%"
IF "%answer%"=="" GOTO ERROR
IF "%answer%"=="S" GOTO SHUTDOWN
IF "%answer%"=="F" GOTO FILE
IF "%answer%"=="C" GOTO COMPILE
::::::::::::::::::::::
:SHUTDOWN
echo "call shutdown.bat"
PAUSE
EXIT
::::::::::::::::::::::
:FILE
echo "call FILE.bat"
PAUSE
EXIT
::::::::::::::::::::::
:COMPILE
echo "call COMPILE.bat"
PAUSE
EXIT
::::::::::::::::::::::
:ERROR
echo "ERROR HAPPEN!"
PAUSE

1 個回答

1
蟹老闆
iT邦大師 1 級 ‧ 2020-11-25 00:13:16
最佳解答

二個地方要修改

@ECHO off
echo ------------------  
echo 1. Shutdown(S)
echo 2. File(F)
echo 3. Compile Program(C)
echo ------------------
::set /p answer = Choose: 變數後面沒有空格
set /p answer= Choose:
echo %answer%
::if %answer% == "S" ( 比對文字應該用以雙引號組成一組
if "%answer%" == "S" (
    call shutdown.bat
) else (
    echo unok
)

一舨如果項選不多及只有單字,可以使用choice 好處是可限制只能輸入指定的符號,輸入錯誤還能發出提示音,後面依條件直接Goto 到指定的位置,不需再用IF來判斷

CHOICE /C YNC /M "按是(Y)、否(N)或取消(C)。"
Goto :%Errorlevel%
:1
Echo 1........
Pause
Goto :EOF
:2
Echo 2........
Pause
Goto :EOF
:3
Echo 3........
Pause
Goto :EOF
echo 謝謝你

我要發表回答

立即登入回答