2017-01-27 78 views
-1

我想做一個文本冒險,但我有點無知如何得到一個if/else語句與變量一起工作。在我的代碼可能犯了一些錯誤:批處理。試圖使文本冒險,如果其他工作

@ echo off 

color 0a 

echo This is a text adventure! to play, read through the story and at certain points you get to make decisions. 

echo Remember, selecting an invalid option will automatically be counted as option 2! 

echo have fun 

pause 

cls 

echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is. 

echo You walk towards the door. It is locked. 

echo 1)Force the door open. 

echo 2) look around for another means of escape. 

set /p escape= 

if %escape% = 1 

cls 
echo you get the door open, but an orc comes in and smashes your face. 

echo get rekt buddy, game over. 

pause 

exit 
else 

goto dungeon 


:dungeon 

cls 

echo well done. 

pause 

exit 
+2

不知所措,請閱讀'if'的幫助(在新的命令提示符窗口中輸入「if /?')? – aschipfl

回答

0

如果您if語句有一個伴隨else語句,你需要使用括號。在其他必須也格式) else (,像這樣:

@ echo off 
color 0a 
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions. 
echo Remember, selecting an invalid option will automatically be counted as option 2! 
echo have fun 
pause 
cls 
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is. 
echo You walk towards the door. It is locked. 
echo 1)Force the door open. 
echo 2) look around for another means of escape. 
set /p escape= 
if "%escape%"=="1" (
    cls 
    echo you get the door open, but an orc comes in and smashes your face. 
    echo get rekt buddy, game over. 
    pause 
    exit 
) else (
    goto dungeon 
) 

exit /b 

:dungeon 
cls 
echo well done. 
pause 
exit 

我已經刪除了美觀的目的坯線;如果你真的想要它們,你可以把它們放回去。我還添加了一個exit /b,因爲有goto後跟你要去的標籤是不好的形式。 if語句中的引號用於防止在用戶不輸入任何內容時腳本中斷。