2017-03-13 74 views
-1

我在批處理文件中工作我的第一場比賽,我不斷收到 的錯誤,''此時'採取的是意外''「。 「採取」是我分配變量「霰彈槍」爲。我試圖做的是,如果用戶沒有「拾起將軍」,他們會留在當前區域,但是如果他們確實拿起它將會移動到另一個「活的」區域。「」在這個時候出乎意料

: barn 

    echo. You enter the barn, it's creeky and it's dark, just as it is outside. 
    echo. 
    echo. You wonder if anything of use is in this barn. 
    echo. 
    echo. Should you check under the staircase to the left or to the right? 
    echo. 
    set /p command = 
    echo. 
    if %command% equ right goto barn_right 
    if %command% equ left goto barn_left_die 

     : barn_left_die 
     if %shotgun% equ taken goto barn_left_live 
     echo. You hear creeking in the floorboards to the right, an infected jumps at you tearing you into pieces. 
     echo. 
     echo. YOU ARE DEAD 
     echo. 
     pause 

      : barn_left_live 
      echo. You hear creeking in the floorboards to the right, you kill the infected that jumps at you with your shotgun! 
      echo. You decide to go to a different building, do you go to the shed or house? 
      echo. 
      set /p command = 
      echo. 
      if %command% = shed goto shed 
      if %command% = house goto house 

     : barn_right 
     echo. You see a glint in the dark, it appears to be the moon light reflecting off the steel of a shotgun barrel. 
     echo. 
     echo. You take the shotgun. 
     echo. 
     set /p shotgun = taken 
     echo. Should you now go to the house, shed, or check underneath the left staircase? 
     echo. 
     set /p command = 
     echo. 
     if %command% = shed goto shed_die 
     if %command% = house goto house_die 
     if %command% = left goto left_staircase_die 
     pause 
+1

的可能的複製[爲什麼是與「回聲%VAR%」沒有字符串輸出使用命令行上的「設置VAR =文本」後?](http://stackoverflow.com/questions/26386697/why- is-no-string-output-with-echo-var-after-using-set-var-text-on-comman) – Mofi

+0

通常我會同意@Mofi,就像你看到的那樣,有太多的問題,獨自一人解決不了他們的困境。 – Compo

回答

2

設置變量時,你不應該有你的=字符的空格兩側,set /p command =將結束在一個名爲%command %變量。以相同的方式設置要採取的變量應該是"shotgun=taken"而不是shotgun= taken,其將%shotgun%設置爲值taken(帶有前導空格)。標籤名稱應該直接跟在:之外,因此:barn_right不是: barn_right。當將%shotgun%設置爲taken時,使用Set而不是Set /P。通常我會建議使用Set /P "command=",但在這種情況下,我會說根本不使用Set /P,而是使用Choice。如果你想檢查匹配,然後使用==而不是=而不是equ

Echo(You enter the barn, it's creeky and it's dark, just as it is outside. 
Echo(
Echo(You wonder if anything of use is in this barn. 
Echo(
Echo(Should you check under the staircase to the left or to the right? 
Echo(
Choice /C LR 
If ErrorLevel 2 GoTo barn_right 

:barn_left_die 
If /I "%shotgun%"=="taken" GoTo barn_left_live 
Echo(You hear creaking in the floorboards to the right, an infected jumps at you tearing you into pieces. 
Echo(
Echo(YOU ARE DEAD 
Echo(
Timeout -1 /NoBreak 

:barn_left_live 
Echo(You hear creaking in the floorboards to the right, you kill the infected that jumps at you with your shotgun! 
Echo(
Echo(You decide to go to a different building, do you go to the shed or house? 
Echo. 
Choice /C HS 
If ErrorLevel 2 GoTo shed 
GoTo house 

:barn_right 
Echo(You see a glint in the dark, it appears to be the moon light reflecting off the steel of a shotgun barrel. 
Echo(
Echo(You take the shotgun. 
Echo(
Set "shotgun=taken" 
Echo(Should you now go to the house, shed, or check underneath the left staircase? 
Echo(
Choice /C LHS 
If ErrorLevel 3 goto shed_die 
If ErrorLevel 2 goto house_die 
GoTo left_staircase_die 
Timeout -1 /NoBreak