本文介绍了文本框结果错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写这段代码

I write this code

int area=0;
      int count=0;
      int error = 0;
      //Comparison>>>>>>>>>>>>>>>>>>>>>>>>>
      for (int x = 0; x < bm3.Width; x++)
      {
          for (int y = 0; y < bm3.Height; y++)
          {
              area += 1;
              Color temval = bm3.GetPixel(x, y);
              Color proval = bm33.GetPixel(x, y);
              if (temval.R != proval.R) count += 1;
          }
      }
      error = count / area;

      textBox1.Text = error.ToString();



在运行时
textbox1中的结果始终为零
尽管count值不为零



at run time
the result in textbox1 is always zero
although count value is not zero

推荐答案

error = count / area;

将始终具有以下两个值之一:0或1.并且1每次都取决于temval.Rproval.R相同.单个差将为零.

考虑使用浮点数或双精度数-可能会得到更明智的结果.

will always have one of two values: 0 or 1. And the 1 is dependant on temval.R and proval.R being the same every time. A single difference will give a zero.

Consider using floats, or doubles instead - you might get a more sensible result.


int a = 5;
int b = 6;
string c = (a / b).ToString();


这将导致包含"0"的字符串.

一种解决方法是使用小数进行计算.如果要将整数保留在循环中,则可以在计算时强制转换变量.例如(继续上一个示例):


That would result to a string containing "0".

One way to handle this is that that you do the calculation using decimals. If you want to keep the integers in your loop, you can cast the variables at calculation time. For example (continuation to the previous example):

c = ((decimal)a / (decimal)b).ToString();


这将导致包含"0,8333333333333333333333333333333"的字符串.


That would result to a string containing "0,8333333333333333333333333333".


if ( condition )
{
   instruction 1;
   instruction 2;
   ....
}



我认为这应该是正确的答案.



I think that this should be the right answer.

int area=0;
int count=0;
double error = 0;
//Comparison
for (int x = 0; x < bm3.Width; x = x + 1)
{
   for (int y = 0; y < bm3.Height; y = y + 1)
   {
       area = area +  1;
       Color temval = bm3.GetPixel(x, y);
       Color proval = bm33.GetPixel(x, y);
       if (temval.R != proval.R)
       {
          count = count + 1;
       }
   }
}
error = count / area;

textBox1.Text = error.ToString("### ##0.00");



祝一切顺利,
PerićŽeljko



All the best,
Perić Željko


这篇关于文本框结果错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:14