我的程序中除以零异常时遇到问题。我尝试了几种技巧,但都没有奏效。我应该在代码中进行哪些更改以避免被零除?
我知道我必须在我的代码的“IF”部分添加一些额外的语句,但我仍然不知道我到底应该做什么。
for (counter = 0; counter < numbers.Length; counter++)
{
string input = Console.ReadLine();
if(string.IsNullOrEmpty(input))
{
break;
}
else
{
int.TryParse (input, out numbers[counter]);
}
}
for (int i = 0; i < numbers.Length; i++) {
sum += numbers [i];
if (numbers [i] % 3 == 0 && numbers [i] % 9 != 0) {
div3not9++;
}
if (numbers [i] >= 11 && numbers [i] <= 131 && (numbers [i] * numbers [i] % 7 == 0) && (numbers [i] * numbers [i] % 3 != 0)) {
range11to131sqdiv7not3++;
SUMrange11to131sqdiv7not3 += numbers [i];
if (range11to131sqdiv7not3 > 0) {
mean = SUMrange11to131sqdiv7not3 / range11to131sqdiv7not3;
}
else
mean = 0;
}
}
for (int y = 0; y < numbers.Length; y++) {
if (numbers [y] > 0)
min = numbers [y];
}
for (int r = 0; r < numbers.Length; r++) {
if (numbers [r] < min && numbers [r] > 0)
min = numbers [r];
if (numbers [r] % min == 0) {
divmin++; // DIVISION BY ZERO EXCEPTION
}
if (sum % numbers [r] == 0) {
divisorsofsum++; // DIVISION BY ZERO EXCEPTION
}
}