Re: How to distinguish odd / even numbers and sumthem
- From: "Tom Leylan" <tleylan@xxxxxxxxxx>
- Date: Wed, 24 Jan 2007 20:29:53 -0500
I guess I sort of stuck my foot in my mouth on that one... now I have to
post something :-) Glad "Lost" took it with humor. What I love about these
homework assignments is that they create small topics from which solutions
can be discussed. Questions like "how do I write a video game" isn't quite
going to be solved by each of us submitting our collision detection routine.
I'd suggest a couple of things. First the routine should be written
independently of the source of input so I wouldn't grab the data out of the
textbox but would require that to be passed to the routine. I opted to
display the values directly into the labels though. The reason is the extra
code needed to initialize them (outside of the routine) and to pass them by
reference just obfuscates things (since (I doubt) this is going to be part
of a reusable library.)
The other was testing the characters as "strings" is problematic. The
solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it must
have been "1,3,5,7 or 9" and I don't know the user didn't key in
"ab383994*0".
I decided to add some functionality that I might be able to use in the
future by creating IsEven() and IsOdd() functions. While not necessary I'd
hate to write ((value MOD 2) = 0 ) the next time I needed to know. Uh,
let's see I opted for the Int32 declaration (not trying to start a fight but
I prefer it too). And I used TryParse so I can determine when non-digits
were entered and I can ignore them.
So here is mine:
' upon some click event or something
ValueCounter(txtInput.Text.Trim())
Private Sub Test7(ByVal sInput As String)
Dim nEvens As Int32 = 0
Dim nOdds As Int32 = 0
Dim bIsDigit As Boolean
Dim nValue As Int32 = 0
For i As Integer = 0 To (sInput.Length - 1)
bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue)
If bIsDigit Then
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
End If
Next
lblEven.Text = "Sum of even numbers is: " + nEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString()
End Sub
Public Function IsEven(ByVal num As Int32) As Boolean
Return ((num Mod 2) = 0)
End Function
Public Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function
"Stephany Young" <noone@localhost> wrote in message
news:usEBuRBQHHA.992@xxxxxxxxxxxxxxxxxxxxxxx
Dim intTotEvens As Integer
Dim intTotOdds As Integer
Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg 23578
For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next
Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()
Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()
Does that at least get me a C+? :)
This should grade higher:
Dim _evens As Integer
Dim _odds As Integer
For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next
Label1.Text = "Sum of odd numbers is: " & _odds.ToString()
Label2.Text = "Sum of even numbers is: " & _evens.ToString()
"Tom Leylan" <tleylan@xxxxxxxxxx> wrote in message
news:%23vG7tDBQHHA.1152@xxxxxxxxxxxxxxxxxxxxxxx
Well that will get him a C- :-)
"Lost" <lostagain@xxxxxxxxxxxxxx> wrote in message
news:45b7ea11$0$97216$892e7fe2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
"Ron" <pts4560@xxxxxxxxx> wrote:
I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...
the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10
any ideas?
thanks
Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer
intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) > 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)
--
Modo vincis, modo peris
.
- Follow-Ups:
- Re: How to distinguish odd / even numbers and sumthem
- From: Bruce W. Darby
- Re: How to distinguish odd / even numbers and sumthem
- From: Stephany Young
- Re: How to distinguish odd / even numbers and sumthem
- References:
- Re: How to distinguish odd / even numbers and sumthem
- From: Tom Leylan
- Re: How to distinguish odd / even numbers and sumthem
- From: Stephany Young
- Re: How to distinguish odd / even numbers and sumthem
- Prev by Date: Insert a textbox string into an Access Database
- Next by Date: Re: Importing .dat file
- Previous by thread: Re: How to distinguish odd / even numbers and sumthem
- Next by thread: Re: How to distinguish odd / even numbers and sumthem
- Index(es):
Relevant Pages
|