Only numbers

Console.WriteLine(" Please enter a Number");
MyNumber1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Please enter a Number");
MyNumber1 = Convert.ToInt32(Console.ReadLine());
how to make if player tried to enter a char to decline and ask him to send a number
4 Replies
eult πŸ‡΅πŸ‡ΈπŸ‡΅πŸ‡Έ
while (beetween != "+" && beetween != "-" && beetween != "*" && beetween != "/")
{
Console.WriteLine(" Please make sure you use");
Console.WriteLine(" + or - or * or /");
beetween = Convert.ToString(Console.ReadLine());
}
while (beetween != "+" && beetween != "-" && beetween != "*" && beetween != "/")
{
Console.WriteLine(" Please make sure you use");
Console.WriteLine(" + or - or * or /");
beetween = Convert.ToString(Console.ReadLine());
}
like this but for a number πŸ™‚
Unknown User
Unknown Userβ€’3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiXβ€’3y ago
The TryParse pattern is considered best practice of parsing data from a string: - a TryParse method returns true or false to inform you if it succeeded or not, so you can use it directly in a condition, - since C# 7 you can declare a variable that will be used as an out argument inline in an argument list, - it forces you to check if the out argument contains valid data afterwards, Avoid: Convert.ToInt32 β€” it's a bad choice for parsing an int. It exists only for backwards compatibility reasons and should be considered last resort.
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
(Note: Convert does contain useful conversion methods: To/FromBase64String, To/FromHexString, ToString(X value, int toBase), ToX(string? value, int fromBase)) Avoid: int.Parse β€” you have to use a try/catch statement to handle invalid input, which is a less clean solution.
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Buddy
Buddyβ€’3y ago
Also, don't use ReadLine, use ReadKey ReadLine reads an entire line, until ENTER has been pressed (as it is named), while ReadKey only reads a certain key.
Want results from more Discord servers?
Add your server