10 c# divide-by-zero
C#新手在这里,当下面的int'max'为0时我得到除零错误,我可以看出为什么会发生这种情况但是当max为0时我应该如何处理呢?位置也是一个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
{
}
}
Run Code Online (Sandbox Code Playgroud)
Sim*_*mon 16
int percent = 0
if (max != 0) percent = (100*position) / max
Run Code Online (Sandbox Code Playgroud)
int percent = ( max > 0 ) ? (100 * position) / max : 0;取决于你想要什么.