How to cancel action with key

Asked

Viewed 80 times

-3

As it cancels an action, the code below is only one example:

    Console.WriteLine("Digite algo");
        string text = Console.ReadLine();
                            switch (){
    case "comando": //

> Aqui seria para cancelar com a tecla ESC, e voltar para a primeira
> linha do código
    Console.WriteLine("Digite algo");
    Console.ReadLine();
    Console.WriteLine("Digite outra coisa");
    Console.ReadLine();
}
  • There are several forms, only with this section it is difficult to help, you have to see how you are doing. Actually I don’t even know if it’s clear what you want in detail. The best way depends on the details.

  • In case it would just cancel the case, and return to the Console.Writeline("Type something"); that’s it

  • 2

    Set cancel the case. programming is detail, if you do not provide details it becomes difficult to help.

  • Type, user entered "command" unintentionally on the console, then he wants to re-enter another case, then would have to cancel for him to type.

  • @Leonardoaraujo The execution will end before the user can type the "cancel command".

1 answer

0


I found your question very confusing, but from the comment you made, I think you want something like the code below:

string text = string.Empty;

do{
    Console.WriteLine("Digite algo");
    text = Console.ReadLine();
} while (text != "alguma_coisa_invalida");

switch (text){
    case "alguma_coisa_valida":
        //todo
        break;
    case "outra_coisa_valida":
        //todo
        break;
    case "mais_uma_coisa_valida":
        //todo
        break;
    default:
        //todo
        break;
}

explaining: the code reads the user input and validates if it is something accepted. If it is, enter the case, if not, still waiting for an entry okay.

  • It’s just that I wish I had another way, but this one will do

  • There’s ways to do it with all the cases I want to put on?

  • @Leonardoaraujo, I edited the answer

  • It seems that you are not reading the other cases, for example: case "alguma_coisa_invalida" appears a message, but case "outra_coisa_valida" no message

  • perhaps the problem is that you do not understand well the functioning of switch. If he goes in first marry It will not enter the second, and so on. Read https://msdn.microsoft.com/pt-br/library/06tc147t(v=vs.100). aspx

Browser other questions tagged

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