How to use Readkeey in C#

Asked

Viewed 137 times

-1

int loop = 0;
while( loop >= 0 )
{
    int aa;
    int an;
    Console.WriteLine( " Qual o Ano Atual? : " );
    aa = Convert.ToInt16( Console.ReadLine() );
    Console.WriteLine( " Qual o Ano de Nascimento? : " );
    an = Convert.ToInt16( Console.ReadLine() );

    int id = aa - an;

    if( id >= 18 )
    {
        Console.WriteLine( " De Maior, sua idade é:" + " " + id );
    }
    else
    {
        Console.WriteLine( " De Menor, sua idade é:" + " " + id );
    }

    Console.Write( " Se não desejar continuar Press <n> para sair... " ); 
    Console.WriteLine();

    while ( Console.ReadKey().Key == ConsoleKey.N )
    {
        Console.ReadKey( true );
    }

} loop++;
  • 1

    It would be interesting to make clear what the doubt is and what part of the code is your difficulty.

  • 1

    What’s the matter with you?

  • OK thank you, I will specify better next time.

2 answers

1

Console.Write("Pressione <Enter> para sair... ");
while (Console.ReadKey().Key != ConsoleKey.Enter) { }
  • 3

    Don’t answer only with code, explain in detail what your code does and how it solves the problem.

1

You can in place of Console.ReadKey(True) use only one break to close the loop is to quit the code.

Console.Write( " Se não desejar continuar Press <n> para sair... " ); 
Console.WriteLine();

if ( Console.ReadKey().Key == ConsoleKey.N )
{
    break;
}

Browser other questions tagged

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