批处理脚本:在 IF 语句上使用 GOTO

R_G*_*osh 2 batch-file

When you get to the user input part, no matter what I type (desk, fire, door) it always goes to fireplace. Is there something wrong with my if syntax?

    @echo off
    color C
    title RG Text Game
    echo -------------------------------------------------
    echo Welcome to the Game!
    echo.
    echo -------------------------------------------------
    echo.
    pause

    echo.
    echo Blah bah blah story story
    echo What do you want to do?
    echo Choices: fire/desk/door
    set /p choice= 
    if %choice%=="fire" GOTO fireplace
    if %choice%=="desk" GOTO desk
    if %choice%=="door" GOTO door

    :fireplace
    echo.
    echo You come to the fireplace.
    echo.
    pause

    :desk
    echo.
    echo You go to the desk.
    echo.

    :door
    echo.
    echo You go to the door.
    echo.
Run Code Online (Sandbox Code Playgroud)

Sac*_*Dee 5

Doublequote %choice% or it will not be equal: desk is not equal as "desk".

And exit your Label block with a goto:eof or exit/b.

使用/iswitch withIF这样你也可以使用DESK或DesK

 if /i "%choice%"=="fire" GOTO fireplace
 if /i "%choice%"=="desk" GOTO desk
 if /i "%choice%"=="door" GOTO door
 goto:error

:fireplace
echo.
echo You come to the fireplace.
echo.
pause
exit/b

:desk
echo.
echo You go to the desk.
echo.
exit/b

:door
echo.
echo You go to the door.
echo.
exit/b
Run Code Online (Sandbox Code Playgroud)