vb.net 显示一系列数字的总和?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22941168/
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
Displaying the sum of a range of numbers?
提问by user3511558
I have to create a program that calculates the sum of a range of numbers entered by the user and displays in a label an expression with the numbers in range. So if I entered "10" as a starting number and "20" as an ending number, there would be a label that displays "10+11+12+13+14+15+16+17+18+19+20".
我必须创建一个程序来计算用户输入的一系列数字的总和,并在标签中显示一个包含范围内数字的表达式。因此,如果我输入“10”作为起始数字并输入“20”作为结束数字,则会有一个显示“10+11+12+13+14+15+16+17+18+19+20”的标签.
This is what I have so far. I'm not sure how to get the range of numbers and display it in a label. I'm also really new to Visual Basic (I'm taking it as a course in high school) so please dumb down your answer as much as possible :) Any help is appreciated! Thanks.
这就是我迄今为止所拥有的。我不确定如何获取数字范围并将其显示在标签中。我对 Visual Basic 也很陌生(我在高中时把它作为一门课程)所以请尽可能地简化你的答案:) 感谢任何帮助!谢谢。
Dim intStartingNumber As Integer = Val(Me.txtStartNumber.Text)
Dim intEndingNumber As Integer = Val(Me.txtEndNumber.Text)
Dim intSum As Integer = 0
Me.lblNumbers.Text = intStartingNumber & "+" & intEndingNumber
For intStartingNumber = Val(Me.txtStartNumber.Text) To intEndingNumber Step 1
intSum = intSum + intStartingNumber
Next
Me.lblNumbersSum.Text = intSum
采纳答案by mjsqu
This should work, albeit I haven't been able to test:
这应该有效,尽管我无法测试:
Dim intStartingNumber As Integer = Val(Me.txtStartNumber.Text)
Dim intEndingNumber As Integer = Val(Me.txtEndNumber.Text)
Dim intSum As Integer = 0
Dim intIndex As Integer
Dim strExpr As String
strExpr = Me.txtStartNumber.Text
'Setting up a new variable called intIndex so that intStartingNumber can stay static
For intIndex = Val(Me.txtStartNumber.Text) To intEndingNumber Step 1
intSum = intSum + intIndex
if intIndex > intStartingNumber Then
strExpr = strExpr & "+" & intIndex
End If
Next
Me.lblNumbersSum.Text = intSum
Me.lblNumbers.Text = strExpr
The idea is that you create a new variable called strExpr to hold the expression and then concatenate using &within the Forloop. That way, as you add on the values arithmetically, you're also adding to the string that shows the calculation being done. I'm hoping that's what you were after.
这个想法是您创建一个名为 strExpr 的新变量来保存表达式,然后&在For循环内使用 using 连接。这样,当您对值进行算术相加时,您也将添加到显示已完成计算的字符串中。我希望这就是你所追求的。
If you get any errors, please comment below and I'll amend the script and explain.
如果你有任何错误,请在下面评论,我会修改脚本并解释。
回答by Joel Coehoorn
If you just want the total:
如果你只想要总数:
Dim StartNumber As Integer = Integer.Parse(txtStartNumber.Text)
Dim EndNumber As Integer = Integer.Parse(txtEndNumber.Text)
lblNumbersSum.Text = Enumerable.Range(StartNumber, EndNumber - StartNumber ).Sum()
If you really want the full text expressions:
如果你真的想要全文表达式:
Dim StartNumber As Integer = Integer.Parse(txtStartNumber.Text)
Dim EndNumber As Integer = Integer.Parse(txtEndNumber.Text)
Dim delimiter As String = ""
Dim expression As New StringBuilder()
For Each number As String IN Enumerable.Range(StartNumber, EndNumber - StartNumber )
expression.Append(delimiter).Append(number)
delimiter = "+"
Next number
lblNumbersSum.Text = expression.ToString()
回答by Andrew Morton
As you are doing this to learn the basics of Basic (hah hah, never heard that one before), I will keep it simple:
当您这样做是为了学习 Basic 的基础知识(哈哈,以前从未听说过)时,我将保持简单:
' convert the input text into numbers
Dim startNumber As Integer = Integer.Parse(txtStartNumber.Text)
Dim endNumber As Integer = Integer.Parse(txtEndNumber.Text)
'TODO: optional - check that endNumber > startNumber
' we are going to put the sum and the text of the summation into
' variables; we might as well start them off with the first values
Dim sum As Integer = startNumber
Dim sumText As String = startNumber.ToString()
' now we just need to use a loop that goes from the second value to the end
For i As Integer = startNumber + 1 To endNumber
' we need to use the value i twice, once as a number...
sum = sum + i
' ... and once as a String
sumText = sumText & "+" & i.ToString()
Next
' show the results to the user
lblNumbersSum.Text = sum.ToString()
lblNumbers.Text = sumText
The default Stepvalue for a For..Nextloop is 1, so we don't need to specify that.
循环的默认Step值For..Next是1,所以我们不需要指定它。
Instead of writing sum = sum + i, we could write sum += i, and similarly for sumText = sumText & "+" & i.ToString()we could write sumText &= "+" & i.ToString(). They are just ways of saving a bit of typing.
sum = sum + i我们可以写sum += i,而不是写,同样地,sumText = sumText & "+" & i.ToString()我们可以写sumText &= "+" & i.ToString()。它们只是节省一点打字时间的方法。
As Jens mentioned, it is usually better to use something called a StringBuilder to build a string in a loop, but I expect you will learn about that later. If you want to learn about it now, you could look at the Remarks section of the StringBuilder documentation.
正如 Jens 所提到的,通常最好使用称为 StringBuilder 的东西在循环中构建字符串,但我希望您稍后会了解这一点。如果您现在想了解它,您可以查看StringBuilder 文档的备注部分。

