Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 程序停止工作(如果未键入数字)_C#_Parsing_Int - Fatal编程技术网

C# 程序停止工作(如果未键入数字)

C# 程序停止工作(如果未键入数字),c#,parsing,int,C#,Parsing,Int,这是我的代码中我想成为故障安全的部分。它很好用。。。如果你按数字。。。但是如果你按下一个字母,程序就会停止工作。。在我关闭程序之前展示了很多有趣的东西。 它是什么代码我需要让它说:“对不起,那不是一个数字” 然后向后滚动,以便可以继续按错的位置 这应该是公平的,但我不记得怎么做了 您需要使用: 正如@siva.k在我的回答中指出的,您也可以在while循环中执行此操作,这样您的程序将循环,直到用户输入有效的格式化整数: int numSeatReserveLucy; if(!int.TryPa

这是我的代码中我想成为故障安全的部分。它很好用。。。如果你按数字。。。但是如果你按下一个字母,程序就会停止工作。。在我关闭程序之前展示了很多有趣的东西。 它是什么代码我需要让它说:“对不起,那不是一个数字” 然后向后滚动,以便可以继续按错的位置

这应该是公平的,但我不记得怎么做了

您需要使用:

正如@siva.k在我的回答中指出的,您也可以在
while
循环中执行此操作,这样您的程序将循环,直到用户输入有效的格式化整数:

int numSeatReserveLucy;

if(!int.TryParse(Console.ReadLine(), out numSeatReserveLucy)) 
{
     Console.WriteLine("You've not entered a number!");
}
您需要使用:

正如@siva.k在我的回答中指出的,您也可以在
while
循环中执行此操作,这样您的程序将循环,直到用户输入有效的格式化整数:

int numSeatReserveLucy;

if(!int.TryParse(Console.ReadLine(), out numSeatReserveLucy)) 
{
     Console.WriteLine("You've not entered a number!");
}

改用
int.TryParse
。此方法返回一个布尔值以指示解析状态。这样,如果返回false,您可以提示用户重新输入

var response = Console.ReadLine(); 

// If above Console.ReadLine gets a valid integer
// the following while loop won't never executed    
while (!int.TryParse(response, out numSeatReserveLucy)) 
{ 
    Console.WriteLine("Sorry, didn't get a number"); 
    response = Console.ReadLine(); 
}

改用
int.TryParse
。此方法返回一个布尔值以指示解析状态。这样,如果返回false,您可以提示用户重新输入

var response = Console.ReadLine(); 

// If above Console.ReadLine gets a valid integer
// the following while loop won't never executed    
while (!int.TryParse(response, out numSeatReserveLucy)) 
{ 
    Console.WriteLine("Sorry, didn't get a number"); 
    response = Console.ReadLine(); 
}

我很震惊:为什么投票失败DWrapped在
中,而
它将返回角色,而无需编写整个lotta代码<代码>变量响应=Console.ReadLine();当(!int.TryParse(response,out numsatreservelucy)){Console.WriteLine(“对不起,没有得到一个数字”);response=Console.ReadLine();}
@siva.k绝对可以,我可以添加你的建议。我很震惊:为什么投反对票DWrapped在
中,而
它将返回角色,而无需编写整个lotta代码<代码>变量响应=Console.ReadLine();虽然(!int.TryParse(response,out numsatreservelucy)){Console.WriteLine(“对不起,没有得到数字”);response=Console.ReadLine();}
@siva.k绝对可以,我可以添加您的建议