由于在任务中我调用了一个方法并且发生了输入错误,所以我受到了惩罚。我不知道如何替换我编写的代码或者使用什么替代方案。我需要帮助找到正确的方法来解决这个问题。
我喜欢编码,所以只需要一个正确的指引! :)
我编写的代码看起来像这样。
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;
}
}
num
没有作为一个类成员的充分理由,而且它作为一个局部变量有几个好处。这是不好的编程风格。 - Ben Voigt