我想在c#中使用TryParse方法。 我正在創建一個程序,它接受來自用戶的輸入,如果它是整數,則返回一個變量。當輸入有效整數時,程序工作正常,但一旦我輸入無效號碼如12sd,它將返回a=0。錯誤在哪裏? public int checkValidNumber()
{
Program obj = new Program();
int a = 0;
string st
我希望能夠阻止人們進入比0和19 使用的TryParse之間的int值其他任何東西,我可以保證比可以只輸入整數值。使用標準的while循環,我可以確保只能輸入0到19之間的整數,但我正在努力合併爲兩個值。 到目前爲止,我通過分離兩個循環來實現它的唯一方式,它看起來很混亂。像這樣: while (!(int.TryParse(Console.ReadLine(), out quant)))
int VillainId = -1;
Console.Write("Enter VillainId: ");
while (!int.TryParse(Console.ReadLine(), out VillainId))
{
Console.WriteLine("You need to enter a valid Villain Id!");