如何在两个数字之间添加偶数? [英] How Do I Add Even Numbers Between Two Numbers Together?

查看:113
本文介绍了如何在两个数字之间添加偶数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文本框和一个按钮。

第一个txtbox是用户输入的第一个数字

第二个txtbox是用户输入的第二个数字

第三个txtbox是总和出现的位置。

表格上还有一个输入按钮。



我可以获得编码方面的帮助吗?

一个简单的3或4行代码会很棒,谢谢!

I have three textboxes, and one button .
The first txtbox is where the first number is entered by the user
The second txtbox is where the second number is entered by the user
The third txtbox is where the sum will appear at.
And a enter button is also on the form.

Can I get some help with the coding please!
A simple 3 or 4 line code will be great, thanks!

推荐答案

不需要循环来执行此操作:

There's no need for a loop to do this:
public static int SumOfEvenNumbersBetween(int start, int end)
{
    if (end < start) 
    {
        // Swap the arguments if necessary:
        return SumOfEvenNumbersBetween(end, start);
    }
    
    if (start < 0)
    {
        // Handle negative ranges:
        if (end < 0) return -SumOfEvenNumbersBetween(-end, -start);
        return SumOfEvenNumbersBetween(0, end) - SumOfEvenNumbersBetween(0, -start);
    }
    
    // The number of evens between 1 and the start of the range (exclusive).
    int m = (start - 1) / 2;
    
    // The number of evens between 1 and the end of the range (inclusive).
    int n = end / 2;
    
    // The sum of the first n even numbers,
    // minus the sum of the first m even numbers:
    return (n * (n + 1)) - (m * (m + 1));
}




Public Shared Function SumOfEvenNumbersBetween(ByVal startNumber As Integer, ByVal endNumber As Integer) As Integer
    If endNumber < startNumber Then
        ' Swap the arguments if necessary:
        Return SumOfEvenNumbersBetween(endNumber, startNumber)
    End If
    
    If startNumber < 0 Then
        ' Handle negative ranges:
        If endNumber < 0 Then
            Return -SumOfEvenNumbersBetween(-endNumber, -startNumber)
        End If
        
        Return SumOfEvenNumbersBetween(0, endNumber) - SumOfEvenNumbersBetween(0, -startNumber)
    End If
    
    ' The number of evens between 1 and the start of the range (exclusive).
    Dim m As Integer = (startNumber - 1) \ 2
    
    ' The number of evens between 1 and the end of the range (inclusive).
    Dim n As Integer = endNumber \ 2
    
    ' The sum of the first n even numbers,
    ' minus the sum of the first m even numbers:
    Return (n * (n + 1)) - (m * (m + 1))
End Function





第一个 n 正数偶数之和 n×(n + 1)



对于正范围,您只需要计算第一个 n 偶数的总和,然后减去第一个 m 偶数的总和,其中 m 是范围开始前的偶数。



对于纯负范围,你只需要交换和否定参数,然后否定答案。例如, -9 -1 之间的偶数之和是偶数之和的负数数字 1 9



适用范围跨越零,将它们分成两部分,应用之前的规则,然后结合结果。



代码看起来比循环/ LINQ答案稍微复杂一点此问题的重新发布版本 [< a href =http://www.codeproject.com/Questions/849378/How-do-I-sum-even-numbers-between-two-numberstarget =_ blanktitle =New Window> ^ ],但对于大范围,它会快得多。



编辑:添加了VB.NET版本的代码匹配标签。



The sum of the first n positive even numbers is n × (n + 1).

For positive ranges, you just need to calculate the sum of the first n even numbers, and then subtract the sum of the first m even numbers, where m is the number of even numbers before the start of your range.

For purely negative ranges, you just need to swap and negate the arguments, and then negate the answer. For example, the sum of the even numbers between -9 and -1 is the negative of the sum of the even numbers between 1 and 9.

For ranges which straddle zero, break them into two parts, apply the previous rules, and then combine the results.

The code looks slightly more complicated than the loop / LINQ answers in your reposted version of this question[^], but for large ranges, it will be much faster.

Added VB.NET version of the code to match the tags.


正如我在评论中提到的那样: 不要重新发布!!!



在这里你得到了答案:如何在两个数字之间求和偶数数 [ ^ ]
As i mentioned in my comment to the question: do not repost!!!

Here you've got your answer: How do I sum even numbers between two numbers[^]


嗨试试这个

Hi Try this
private void button2_Click_1(object sender, EventArgs e)
     {
         textBox3.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text));
     }


这篇关于如何在两个数字之间添加偶数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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