iT邦幫忙

0

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

  • 分享至 

  • xImage

救救我!/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邦高手 2 級 ‧ 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
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

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
EricChu iT邦新手 4 級 ‧ 2020-11-25 08:26:15 檢舉
echo 謝謝你

我要發表回答

立即登入回答