【发布时间】:2016-05-25 12:29:53
【问题描述】:
public static List<int> getDenoms(long n)
{
List<int> result = new List<int>();
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
result.Add(i);
}
}
return result;
}
public static int getHighestPrime(List<int> seq)
{
int currentHigh = 1;
foreach (int number in seq)
{
List<int> temp = getDenoms(number);
if (temp.Count == 1)
{
if (number > currentHigh)
{
currentHigh = number;
}
}
}
return currentHigh;
}
我有当前的 C# 代码。所以,我有两种方法。在 getDenoms 方法中,我假设语句 n%i 不会抛出任何错误,因为 i 大于或等于 1,但它确实会抛出该错误。
我以如下方式使用了这两种方法:
Console.WriteLine(getHighestPrime(getDenoms(600851475143)));
对代码抛出该错误的原因有任何见解吗?
【问题讨论】:
标签: c# divide-by-zero