2017-01-27 1 views
-1

Ich versuche, ein Text-Abenteuer zu machen, aber ich bin irgendwie ahnungslos, wie man eine If/Else-Anweisung mit einer Variablen arbeiten. wahrscheinlich ein paar Fehler in meinem Code:Charge. Versuchen, Text Abenteuer zu machen, wenn es nicht funktioniert

@ 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

Haben Sie sich jemals die Hilfe von 'if' gefragt (geben Sie' if /? 'In ein neues Eingabeaufforderungsfenster ein)? – aschipfl

Antwort

0

Wenn Ihr if Anweisung eine begleitende else Aussage hat hat, müssen Sie Klammern verwenden. Die sonst muss auch ) else (, wie dies im Format:

@ 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 

Ich habe die Zuschnitte Linien für ästhetische Zwecke entfernt; Sie können sie wieder einsetzen, wenn Sie sie wirklich wollen. Ich fügte auch eine exit /b hinzu, weil ein goto gefolgt von dem Etikett, das du gehst, schlechte Form ist. Die Anführungszeichen in der if-Anweisung sollen verhindern, dass das Skript bricht, wenn der Benutzer nichts eingibt.

Verwandte Themen