2016-07-25 62 views
0

我想在Repeat Until循環(Pascal。)中使用多個if/else語句我得到一個錯誤,指出編譯器期望Before語句在第一個If子句之後完成。在Repeat Until循環中使用多個if/else語句

如何將其他if/else語句嵌套到循環中?

program adventureTime; 
uses crt; (** for debug purposes. it allows the use of the readkey function **) 

var 
    playerName, inputAnswer : string; 
    gameCounter : integer; 

begin 
    (** INTRODUCTION **) 
    gameCounter := 0; 

    repeat 
      writeln('Hello adventurer. What is thy name? '); 
      readln(playerName); 

      writeln('It is nice to meet you ', playerName, '.'); 
      writeln('Are you ready for an adventure? (yes/no/maybe)'); 

      readln(inputAnswer); 

      if (inputAnswer = 'no') then 
        writeln; 
        writeln('Wrong answer! Try again!'); 
        writeln; 
        gameCounter := 0; 

      else if (inputAnswer = 'yes') then 
        writeln; 
        writeln('Great! Get ready for the adventure of a lifetime!'); 
        writeln; 
        gameCounter := 2; 

      else if (inputAnswer = 'maybe') then 
        writeln; 
        writeln('Make up your mind, fool!'); 
        writeln; 
        gameCounter := 0; 

      else 
        writeln; 
        writeln('That was not one of the options!'); 
        writeln; 
        gameCounter := 0; 

    until gameCounter <> 0; 


    writeln('out of bounds'); 
    readkey; 

end. 
+0

參見[這個答案](http://stackoverflow.com/a/28221465/62576)有關if和begin/end的教程。 –

+0

[Pascal適當的結構語法,如果開始結束和; (在Inno Setup中)](http://stackoverflow.com/questions/28221394/proper-structure-syntax-for-pascal-if-then-begin-end-and-in-inno-setup) –

+0

謝謝,肯!我能夠通過將您發送的教程與此帖子中的其他答案相結合來解決我的問題。 – qmesa

回答

0

不知道,但沒有一個如果帕斯卡這個樣子的else語句:

if ... then 
begin 
. 
. 
. 
end 
else if... 
+0

查看freepascal文檔(http://www.freepascal.org/docs-html/current/ref/refsu58.html#x163-18500013.2.3),我不清楚這是否是這種情況。該文檔建議(在buttom的示例中)不必爲每個if/else子句實施end子句。 我希望實現一個重複循環,它圍繞所有if else子句「包裝」(因爲不要爲每個if/else語句使用末尾子句)。 – qmesa

+1

我能夠通過使用您的評論和Ken的教程鏈接來解決問題。通過開始和結束單獨的if/else語句,語法是正確的,代碼編譯順利。謝謝! – qmesa

+0

很高興能夠幫助你!在檢查條件之後,似乎只能在執行單個語句時省略「開始」和「結束」。 – user6454491