C# 除以零错误,我该如何解决这个问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/160141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:11:58  来源:igfitidea点击:

Divide by zero error, how do I fix this?

提问by

C# novice here, when the int 'max' below is 0 I get a divide by zero error, I can see why this happens but how should I handle this when max is 0? position is also an int.

C# 新手,当下面的 int 'max' 为 0 时,我得到除以零错误,我可以理解为什么会发生这种情况,但是当 max 为 0 时我应该如何处理?位置也是一个整数。

    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
        {
        }
    }

回答by Simon

int percent = 0
if (max != 0) percent = (100*position) / max

回答by palehorse

Check for zero.

检查零。

if ( max == 0 ) {
    txt = "0%";
} else {
    // Do the other stuff....

回答by Adam Wright

Well, that entirely depends on the behaviour you want. If the maximum value of your program bar is zero, is it full? Is it empty? This is a design choice, and when you've chosen, just test for max == 0 and deploy your answer.

嗯,这完全取决于你想要的行为。如果您的程序栏的最大值为零,是否已满?是空的吗?这是一个设计选择,当您选择时,只需测试 max == 0 并部署您的答案。

回答by Swati

  • You can throw an exception.
  • You can do int percent = ( max > 0 ) ? (100 * position) / max : 0;
  • You can choose to do nothing instead of assigning a value to percent.
  • many, many other things...
  • 您可以抛出异常。
  • 你可以做 int percent = ( max > 0 ) ? (100 * position) / max : 0;
  • 您可以选择什么都不做,而不是为百分比分配一个值。
  • 很多很多其他的东西......

Depends on what you want.

取决于你想要什么。

回答by Esteban Araya

This is not a C# problem, it's a math problem. Division by zero is undefined. Have an if statement that checks whether max > 0 and only execute your division then.

这不是 C# 问题,而是数学问题。除以零是未定义的。有一个 if 语句来检查 max > 0 是否然后只执行你的除法。

回答by Marcin

Well, if max is zero, then there is no progress to be made. Try catching the exception where this is called. That is probably the place to decide whether there is a problem or if the progress bar should be set at zero or at 100%.

好吧,如果 max 为零,那么就没有进展。尝试捕获调用 this 的异常。这可能是决定是否存在问题或进度条是否应设置为零或 100% 的地方。

回答by Bob King

I guess the root question is: Does it make sense to even call this function where max is '0'? If yes, then I'd add special handling to it i.e.:

我想根本问题是:甚至在 max 为“0”的情况下调用这个函数是否有意义?如果是,那么我会为其添加特殊处理,即:

if (max == 0) 
{
    //do special handling here
}
else
{
    //do normal code here
}

If 0 doesn't make sense, I'd investigate where it's coming from.

如果 0 没有意义,我会调查它的来源。

回答by ckramer

You would need a guard clause which checks for max == 0.

您需要一个检查 max == 0 的保护子句。

private void SetProgressBar(string text, int position, int max)
{
    if(max == 0)
        return;
    int percent = (100 * position) / max; //when max is 0 bug hits
    string txt = text + String.Format(". {0}%", percent);
    SetStatus(txt);
}

You could also handle the Divide by Zero exception, as your sample showed, but it is generally more costly to handle exceptions then to set up checks for known bad values.

您还可以处理除零异常,如您的示例所示,但处理异常通常比设置检查已知错误值的成本更高。

回答by Dre

If you are using this for a download, you'll probably want to show 0% as I assume max would == 0 in this case when you don't KNOW the file size yet.

如果您使用它进行下载,您可能希望显示 0%,因为我假设在这种情况下当您还不知道文件大小时 max 将 == 0。

int percent = 0;
if (max != 0)
    ...;

If you are using this for some other long task, I'd want to assume 100%

如果您将它用于其他一些长期任务,我想假设 100%

But also, since position can never be between 0 and -1, so you'll probably want to drop the 100 *

而且,由于位置永远不会介于 0 和 -1 之间,因此您可能想要删除 100 *

回答by tzot

Convert your

转换您的

int percent = (100 * position) / max;

into

进入

int percent;
if (max != 0)
    percent = (100 * position) / max;
else
    percent = 100; // or whatever fits your needs