You have to convert the value to integer, see:
using System;
public class Program
{
public static void Main()
{
int tempo = 0;
if (int.TryParse(Console.ReadLine(), out tempo))
Console.WriteLine("O tempo {0} foi adicionado", tempo);
else
Console.WriteLine("Valor inteiro inválido");
}
}
Entree:
10
Exit:
Time 10 has been added
The method TryParse
does it for you.
See working on .NET Fiddle.
Editing
After clearing the AP’s doubt in chat I worked out this solution:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
Console.WriteLine("Comando: ");
var comando = Console.ReadLine();
if (VerrificarComando(comando))
{
var tempo = 0;
if (int.TryParse(Regex.Match(comando, @"\d+").Value, out tempo))
{
Console.WriteLine("O tempo {0} foi adicionado", tempo);
return;
}
Console.WriteLine("Valor inteiro inválido");
}
Console.WriteLine("Comando inválido");
}
static bool VerrificarComando(string comando)
{
return Regex.Match(comando, @"\b(adicionartempo)\b").Success;
}
}
See working on .NET Fiddle.
Can it be in this format?: add time;(any number he has typed) In case the person would have to type add time; before typing the number
– user92401
@Betadarknight you want a function, like
AdicionarTempo()
?– gato
No, the person would have to type add-time; before typing the number to be executed
– user92401
@Betadarknight as if it were some kind of option?
– gato
Yes, example: additiontime1;(time 1), additiontime2;(time 2) and so on
– user92401
@Betadarknight gives a look, I edited the answer.
– gato
Wouldn’t you be able to put everything together? When writing the add time already add time?
– user92401
But time is informed by the user or is a random value?
– gato
Informed by the user
– user92401
But that’s what the program does, the user enters the add-time command and then informs the time.
– gato
Let’s go continue this discussion in chat.
– user92401