Logical expression for (While or If) Create repetition until the user gets the answer right

Asked

Viewed 886 times

0

I am having problems in a question to define the result of a question in Visualg, so that if the user answers the wrong way the question will repeat until the user gets it right.

The question is: "if this agreement answers with (s) for yes or (n) for no".

Here’s the code I created:


inserir a descrição da imagem aqui

var
  RESPOSTA: CARACTERE
inicio
        Enquanto RESPOSTA <> "n" faca
        Escreva("Esta de acordo?:")
        Leia(RESPOSTA)
          Se RESPOSTA <> "n" entao
            Escreval
            Escreval("Resposta incorreta.")
            Escreval("Vamos tentar novamente.")
            Escreval
            Escreval("Lembre-se!")
            Escreval("Para Responder SIM, Digite a LETRA (s), ou Digite a letra (n) para Responder Não.")
            Escreval("Boa Sorte!")
            Escreval
          Fimse
          Se RESPOSTA = "n" entao
            Escreval
            Escreva("Parabéns, voçê acertou!")
          Fimse
        Fimenquanto
fimalgoritmo

The problem is I only managed to put the lyrics n to function as "No". I wonder if there’s any way I can put the lyrics in too s for the answer "Yes".

This is all in a way that the incorrect reply phrase repeats until the user hits the answer so that it stays N not to and S for yes.

  • Ola Kellvem, well, I don’t know if you are taking a course that requires the use of visualG, but a very good tool for those who are learning to program is Portugol Studio, has much more functionality than visualG, even allows the creation of graphic parts on the screen, You can develop your first game and everything! good studies.

1 answer

1

Your optimized code will look like this:

var
  RESPOSTA: CARACTERE
  CONDICAO: LOGICO
inicio
      CONDICAO <- VERDADEIRO


      ENQUANTO CONDICAO = VERDADEIRO FACA
           Escreval("==========================================")
           Escreval("               Lembre-se!                 ")
           Escreval("         Use LETRAS MAIUSCULAS            ")
           Escreval("  Para Responder SIM, Digite a LETRA (S)  ")
           Escreval("ou Digite a letra (N) para Responder Não. ")
           Escreval("               Boa Sorte!                 ")
           Escreval("==========================================")
           Escreval("    ")


      Escreval("Esta de acordo?: ")
        Leia(RESPOSTA)
        CONDICAO <- FALSO

      ESCOLHA RESPOSTA
         caso "s"
           Escreval("!!!!!!!!!!!!!!!!!!!!!!!")
           escreval("Parabéns, você acertou!")
           Escreval("!!!!!!!!!!!!!!!!!!!!!!!")

         caso "n"
           Escreval("***************************")
           Escreval("    Resposta incorreta.    ")
           Escreval("***************************")
           Escreval("                           ")
           Escreval("  Vamos tentar novamente.  ")
           Escreval("                           ")
           Escreval("                           ")
           Escreval("                           ")

           CONDICAO<- VERDADEIRO

        outrocaso
           Escreval("                           ")
           Escreval("                           ")
           Escreval("############################################")
           Escreval("    Você  digitou um caracter inválido     ")
           Escreval("############################################")
           Escreval("                           ")
           Escreval("                           ")
           CONDICAO<- VERDADEIRO

           fimescolha

      FIMENQUANTO

fimalgoritmo

I chose to use the command CHOICE-CASE instead of the command THEN, because it will be more complex to implement your pseudocode using this command (if-then).

However, it is worth mentioning that the optimization of pseudocode is performed in numerous ways, it may be that you find, on the Internet or with your teacher, the solution of your problem with other commands, however fully functional.

The operation of the CHOICE-CASE control is described below:

The reserved words of this instruction are escolha, caso and fimescolha, the use of outrocaso, as with the instruction se... entao who can use the senao (composite selection) or não (simple selection).

It is important to note that this selection instruction already has its relational operation defined and may not be used other than equality, which is implicit in this instruction (the symbol never appears = in the instruction). Through it it is only possible to verify if there is equality between an integer value or desired character that is wanted evaluate, where its veracity will result only in the execution of other instructions available in the case that corresponds to the value equal to the rated.

If none of the values are specified as a case of this multiple choice instruction will be executed the available instructions in the outrocaso, if the same exists, or the algorithm will continue its execution after the fimescolha.

General Form of Syntax

escolha (<identificador>)
  caso <valor_1>
    primeiro bloco de instrução
  caso <valor_2>
    segundo bloco de instrução
  caso <valor_3>
    terceiro bloco de instrução
  outrocaso
    bloco de instrução do outro caso (valor diferente de todos anteriores)
fimescolha
// continua as instruções do algoritmo em execução

The spelling <valor_1>, <valor_2> and <valor_3> correspond to possible integer values or character that will be compared to the value which is stored in what will be described at the beginning of this instruction, just after the reserved word choice, being the even in parentheses.

Thus, the relational operation of equality will be carried out without its symbol is explicitly described in the instruction

Flowchart:

inserir a descrição da imagem aqui

Source: Control Structures in pseudocode

Browser other questions tagged

You are not signed in. Login or sign up in order to post.