显示一系列数字的总和?

时间:2014-04-08 15:14:43

标签: vb.net

我必须创建一个程序来计算用户输入的数字范围的总和,并在标签中显示一个数字在范围内的表达式。所以,如果我进入" 10"作为起始编号和" 20"作为结束号码,会有一个标签显示" 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20"。

这是我到目前为止所拥有的。我不确定如何获取数字范围并将其显示在标签中。我对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

3 个答案:

答案 0 :(得分:2)

如果您只想要总数:

Dim StartNumber As Integer = Integer.Parse(txtStartNumber.Text)
Dim EndNumber As Integer = Integer.Parse(txtEndNumber.Text)

lblNumbersSum.Text = Enumerable.Range(StartNumber, EndNumber - StartNumber ).Sum()

如果你真的想要全文表达式:

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()

答案 1 :(得分:0)

这应该有效,尽管我无法测试:

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

我们的想法是创建一个名为strExpr的新变量来保存表达式,然后在&循环中使用For连接。这样,当您在算术上添加值时,您还要添加到显示正在进行的计算的字符串。我希望那就是你所追求的。

如果您收到任何错误,请在下面发表评论,我将修改脚本并进行解释。

答案 2 :(得分:0)

当你这样做是为了学习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

Step循环的默认For..Next值为1,因此我们不需要指定该值。

我们可以写sum = sum + i而不是写sum += i,而sumText = sumText & "+" & i.ToString()类似,我们可以写sumText &= "+" & i.ToString()。它们只是节省一些打字的方法。

正如Jens所提到的,通常最好使用一个名为StringBuilder的东西在循环中构建一个字符串,但我希望你稍后会了解它。如果您想立即了解它,可以查看StringBuilder documentation的备注部分。