【发布时间】:2008-10-01 22:55:02
【问题描述】:
这里的 C# 新手,当下面的 int 'max' 为 0 时,我得到除以零的错误,我知道为什么会发生这种情况,但是当 max 为 0 时我应该如何处理呢? position 也是一个 int。
private void SetProgressBar(string text, int position, int max)
{
try
{
int percent = (100 * position) / max; //when max is 0 bug hits
string txt = text + String.Format(". {0}%", percent);
SetStatus(txt);
}
catch
{
}
}
【问题讨论】:
-
您可以使用像stackoverflow.com/a/13260217/378115这样的安全分割方法
标签: c# divide-by-zero