You can implement a method that reads exclusively numbers pressed within the Console
.
For this you will have to start a loop and have to test each iteration if a key was pressed with Console.KeyAvailable
which returns a boolean indicating if there has been the pressing of a key.
If Console.KeyAvailable
To charge a keystroke is to instruct the reading of the key with Console.ReadKey();
passing by true
as the first parameter to prevent the read key from being printed on the 'Console' screen'.
Then test the nature of the pressed key:
If it’s a number accumulate in the string
return method.
If the enter key ends the iteration.
If the back space key removes the last character from the console (see code settings).
Any other value ignore.
Using switch case when
with the syntax of version 7 or higher of C#
:
using System;
class Exemplo
{
public static string lerNumeros()
{
ConsoleKeyInfo cki;
string entrada = "";
bool continuarLoop = true;
while (continuarLoop)
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true);
switch (cki.Key)
{
case ConsoleKey.Backspace:
if (entrada.Length == 0) continue;
entrada = entrada.Remove(entrada.Length - 1);
Console.Write("\b \b"); //Remove o último caractere digitado
break;
case ConsoleKey.Enter:
continuarLoop = false;
break;
case ConsoleKey key when ((ConsoleKey.D0 <= key) && (key <= ConsoleKey.D9) ||
(ConsoleKey.NumPad0 <= key) && (key <= ConsoleKey.NumPad9)):
entrada += cki.KeyChar;
Console.Write(cki.KeyChar);
break;
}
}
return entrada;
}
public static void Main()
{
Console.WriteLine("Insira o valor para X.");
int x = int.Parse(lerNumeros());
Console.WriteLine("\nO valor de X é '{0}'.", x);
Console.ReadKey();
}
}
Using if
independent of the language version:
using System;
class Exemplo
{
public static string lerNumeros()
{
ConsoleKeyInfo cki;
string entrada = "";
while (true)
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Backspace){
if (entrada.Length == 0) continue;
entrada = entrada.Remove(entrada.Length - 1);
Console.Write("\b \b"); //Remove o último caractere digitado
}
if (cki.Key == ConsoleKey.Enter)
{
break;
}
if ((ConsoleKey.D0 <= cki.Key) && (cki.Key <= ConsoleKey.D9) ||
(ConsoleKey.NumPad0 <= cki.Key) && (cki.Key <= ConsoleKey.NumPad9))
{
entrada += cki.KeyChar;
Console.Write(cki.KeyChar);
}
}
return entrada;
}
public static void Main()
{
Console.WriteLine("Insira o valor para X.");
int x = int.Parse(lerNumeros());
Console.WriteLine("\nO valor de X é '{0}'.", x);
Console.ReadKey();
}
}
Code in the Repl.it
Thanks a lot for the resolutions, I managed to implement in my code, but I’m having difficulties to delete the key that was typed, in case of wrong click. I changed this code to recognize the bad Backspace it crashes the console.
– Gabriel.ps2
But this comment is directed to whom? To me or to Daniel. When so do the comment in the reply by pressing the button
comentar
below the reply, that thus the author of the reply will be notified.– Augusto Vasques
If it’s C# why the C tag?
– anonimo