How can I make a repeat...until structure that has multiple exit parameters?

StackOverflow https://stackoverflow.com/questions/21401923

  •  03-10-2022
  •  | 
  •  

Вопрос

So my code looks like this

**Procedure DisplayMenu;
  Begin
    Repeat
      Writeln('Hello, ' ,PlayerName);
      Writeln('1. Be Guesser');
      Writeln('2. Let The Computer Guess');
      Writeln('9. Quit');
      Readln(MainMenuChoice);
    *Until MainMenuChoice=(1) or (2) or (9);*
  End; //Procedure//**

And I would be very grateful if someone could explain to me where I have gone wrong.

I think the error is with the Until MainMenuChoice=(1) or (2) or (9); part.

Это было полезно?

Решение

You are comparing the value of MainMenuChoice to the value created by OR'ing the bits of the three test values together, which produces 11. So your code is actually doing this:

Until MainMenuChoice = 11; 

To compare it again the three individual values, use a Set instead:

Until MainMenuChoice in [1, 2, 9];

However, assuming MainMenuChoice is a Char then Readln() is going to return the character that the user actually typed, not its numeric value, so use this instead:

Until MainMenuChoice in ['1', '2', '9'];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top