Reputation: 5
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".
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.
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
Upvotes: 0
Views: 8499
Reputation: 5452
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 For
loop. 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.
If you get any errors, please comment below and I'll amend the script and explain.
Upvotes: 0
Reputation: 25013
As you are doing this to learn the basics of Basic (hah hah, never heard that one before), I will keep it simple:
' 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 Step
value for a For..Next
loop is 1
, so we don't need to specify that.
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.
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.
Upvotes: 0
Reputation: 415725
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()
Upvotes: 2