我的手腕被打了一巴掌,因为在一个作业中,当输入错误发生时,我有一个方法调用本身。我不知道如何或使用什么来代替我编写的代码。我需要帮助才能找到正确的方法。
我喜欢编码,所以我只需要以正确的方式轻推!:)
我写的代码是这样的。
private void SumTheNumbers()
{
Console.Write("Please give the value no "+ index + " :");
if (false == int.TryParse(Console.ReadLine(), out num))
{
//Errormessage if the user did not input an integer.
Console.WriteLine("Your input is not valid, please try again.");
Console.WriteLine();
sum = 0;
SumTheNumbers();
}
else
{
//Calculate the numbers given by user
sum += num;
}
}