文本框结果错误... [英] Textbox result error ...

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

问题描述

我写这段代码

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

推荐答案

Count可能不为零,但它总是小于或等于area,因为area总是递增,而count为有条件地增加.由于count和area都是整数,因此
Count may not be zero, but it will always be less than or equal to area, since area is always incremented, and count is conditionaly incremented. Since count and area are both integers,
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.


首先,它看起来面积总是大于或等于count.这意味着除法的结果小于或等于1,我想通常小于1.

现在,您使用的数据类型是整数.整数不能包含小数.因此,结果被截断,仅保留整数零.请考虑以下内容:
Firstly it looks that area is always greater or equal than count. This means that the result from the division is less or equal than 1, I''d guess that typically less than 1.

Now, the data types you use are integers. An integer cannot contain decimal. Because of this the result is truncated and only the whole number zero remain. Consider the following:
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".


你好,
我已经根据您显示的代码写了这个答案.
这很重要,因为我看不到bm3和bm33的声明.
似乎您正在尝试检查两个位图对象b3和b33的像素颜色是否相等.
仅当它们的大小相同或b3小于b33时,此代码才有效.
我使用#develop C#,有时我的陈述存在问题,例如y ++或area + = 1,
所以我通常采用最简单的方法y = y + 1或area = area +1.

您在声明错误变量时遇到一些逻辑错误.
错误应该不是double而不是int吗?
在这种情况下,请使用 error.ToString(字符串格式),以便该数字可以在文本框中显示为实数(123,25或245 678,32等).

对于其他类型的字符串格式,您应该访问下面编写的网址.

如何在窗口中设置文本框值.....应用程序 [ ^ ]

同样,如果声明应该像这样:

Hello,
I have wrote this answer based on yours shown code.
This is important because I do not see the declaration of bm3 and bm33.
It seems that you are trying to check equality of pixel color of two bit map objects b3 and b33.
This code should work only if they are the same size or b3 is less than b33.
I use #develop C# where I have sometimes problem with statment as y++ or area +=1,
so I usually go the easiest way y = y + 1 or area = area + 1.

You have some logical mistake in declaration of error variable.
Should not error be double instead of int ?
In that case use error.ToString(string format) so the number can be shown in the text box as real number (123,25 or 245 678,32 etc).

For different kind of string format you should visit the url written below.

how to formate textbox value .....in window application[^]

Also the if statment should go like this :

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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆