显示更大的数字 [英] Displaying the bigger number

查看:78
本文介绍了显示更大的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我希望有人可以提供帮助.

我目前有3个文本框,这些文本框是从Web客户端获取数据的.第一个文本框显示一个数字,第二个文本框在1秒钟后显示相同的来源,以提供不同的数字.然后,第三个框彼此取一个,以得到每秒的数字. (如下textbox8)

我遇到的问题是数字波动,我需要将它达到的最大值存储在第四个文本框中(以下为textbox9)

因此,如果textbox9正在记录在textbox8中生成的最高数字,而textbox8中的实际数字低于先前记录的数字,则它将跳过它,否则它将记录它.

我尝试使用以下代码:

Hello, i am hoping someone can help.

I currently have 3 text boxes which are fetching data from a webclient. The first textbox displays a number, the second displays the same source after 1 second to give a differnt number. The third box then takes one from the other to give a figure per second. (below as textbox8)

The problem i am having is that number fluctuates and i need to store the highest value it reaches in a fourth textbox (Below as textbox9)

So if textbox9 is recording the highest number generated in textbox 8 and actual number in textbox8 is lower than previously recorded it skips it, otherwise it will record it.

I have tried with the following code:

If TextBox9.Text > TextBox8.Text Then
            GoTo bob
        Else : TextBox9.Text = TextBox8.Text
        End If
Bob:



但是可以这么说,它只是读取整数中的第一个数字.
例如,如果textbox9读取999,而textbox8为1000,则它不会更改.
它似乎取了1000中的第一个数字(即"1"),并决定它小于999中的第一个"9".
有人可以帮我证明"1000"大于"999"吗?

对于"9",在10-89的数字范围内的第一个数字也被认为小于"9"的情况也是如此,因为它仅读取整数的第一个数字. (即89中的8小于9).除此之外,它确实可以在一定程度上告诉44大于31.它再次读取第一个数字.

有人知道了吗?

在此先感谢.

哦,这个数字不是小数(它是一个整数).输出是一个字符串,数字可以加/减.

再次感谢



But it is only reading the first number of the whole number so to speak.
Example if textbox9 reads 999 and textbox8 is 1000 it will not change.
It seems to take the first number (i.e. "1") from the 1000 and decides it is smaller than the first "9" from 999.
Can anyone help me to show that "1000" is bigger than "999"?

This is also the case with "9" as the first number in the number range of 10-89 is deemed to be smaller than "9" as it only reads the first number of the whole number. (ie the 8 from 89 is smaller than 9). Apart from that it does work in part where it can tell 44 is bigger than 31. Again it reads the first number.

Anyone got a clue?

Thanks In Advance.

Oh and the number is not a decimal (its a whole number). The output is a string and the numbers add/subtract ok.

Thanks again

推荐答案

您所拥有的以下代码:
This bit of code that you have:
TextBox9.Text > TextBox8.Text


正在比较字符串值.它不知道您要比较数字,因此它取"1000",并且知道"1"在"9"之前,因此认为"1000"小于"999".您要执行的操作是先将文本转换为数字,然后进行比较.就个人而言,我喜欢使用自己的转换功能:


is comparing string values. It doesn''t know that you are comparing numbers, so it takes "1000" and it knows that "1" comes before "9" so it thinks that "1000" is less than "999". What you want to do is first convert the text into a number and then do the compare. Personally, I like to use my own conversion function:

Public Function CvtStrDec(ByVal str As String) As Decimal
        If str Is Nothing Then Return 0
        If str Is DBNull.Value Then Return 0
        If str.Trim.Length = 0 Then Return 0
        If Not IsNumeric(str) Then Return 0
        Return CDec(str)
End Function



然后您可以像这样使用它:



And then you''d use it like this:

cvtStrDec(TextBox9.Text) < cvtStrDec(TextBox8.Text)



它将适用于任何数字,但是如果您真的需要将其作为整数,则可以执行以下操作:



That will work for any number, but if you really need it as an integer you can do something similar with this:

Public Function CvtStrInt(ByVal str As String) As Integer
       If str Is Nothing Then Return 0
       If str Is DBNull.Value Then Return 0
       If str.Trim.Length = 0 Then Return 0
       If Not IsNumeric(str) Then Return 0
       Return CInt(str)
End Function



同样,养成将文本框重命名为更有意义的名称的习惯也是一个好主意.您可能需要研究一些良好的命名约定做法.



Also, it''s a good idea to get into the habit of renaming your textboxes to more meaningful names. You might want to look into some good naming convention practices.


如果要进行字符串比较,请比较字符串.如果您需要任何其他形式的比较,请比较其他对象.

在您的情况下,您想比较数字,尤其是整数,因此比较数字而不是字符串.
"1000"小于"999",因为字符集中的"1"在"9"之前.

而是尝试:
If you want a string comparison, compare strings. If you want any other form of comarison, compare other objects.

In your case, you want to compare numbers, specifically integers, so compare numbers not strings.
"1000" is less than "999" because ''1'' is before ''9'' in the character set.

Instead try:
If int.Parse(TextBox9.Text) > int.Parse(TextBox8.Text) Then
            GoTo bob
        Else : TextBox9.Text = TextBox8.Text
        End If
Bob:


然后,您可以做另外两件事:
1)忘记您曾经听说过的goto,除非有3年的经验,否则请不要再次使用它.然后,您将有机会知道何时使用它是一个好主意.不过,您可能仍然不会遇到任何情况.这可能是我多年来看到的不去使用的最糟糕的例子.替换您的代码:


Then, you can do two other things:
1) Forget you ever heard about goto, and do not use it again until you have three years experience. Then you will stand a chance of knowing when it is a good idea to use it. You probably still won''t come across any cases when you need to though. That is probably the worst example of how not-to-use-goto I have seen in ages. Replace your code:

If int.Parse(TextBox9.Text) <= int.Parse(TextBox8.Text) Then
        TextBox9.Text = TextBox8.Text
End If



2)停止使用默认的VS名称,而改用明智的名称.今天您可能还记得哪个文本框具有最大值,但您还记得下周吗?而是将其命名为tbMaxValue,您不必记住...



2) Stop using the default VS names and use sensible ones instead. You may remember today which text box has the maximum value, but will you remember next week? Call it tbMaxValue instead and you don''t habve to remember...


您正在比较字符串而不是数字,需要将文本框的文本转换为数字,然后比较它们.
You are comparing strings and not numbers, you need to convert the textboxes'' text into a number and only then compare them.


这篇关于显示更大的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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