2017-01-27 9 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に続いてラベルを付けたのは悪いフォームなので、私はexit /bを追加しました。 ifステートメントの引用符は、ユーザーが何も入力しなかった場合にスクリプトが破損するのを防ぐためのものです。