|
-
Jun 18th, 2007, 01:57 PM
#1
Thread Starter
New Member
[2003] Can someone help review this code? [Resolved]
I'm just learning Visual Basic .net, taking a class at the local acommunity college, so give me a break . Here was the assignment "Create an application that allows the user to enter the gender (either F or M) and GPA for one or more students. The application should calculate the average GPA for all students, the average GPA for male students, and the average GPA for female students." (Note, the below code is in a button click). When I click the button after inserting correct info into the text boxes, it just puts that info directly into the male or female label, and never increases/accumulates/whatever in the average GPA labels. Any suggestions? What am I doing wrong?
Thanks, Poppe.
Code:
'declare variables and constants
Dim strGender As String
Dim decGPA As Decimal
Dim intNumMale As Integer 'counter
Dim intNumFemale As Integer 'counter
Dim decGPAMale As Decimal
Dim decGPAFemale As Decimal
Dim decGPAMaleTotal As Decimal 'accumulator
Dim decGPAFemaleTotal As Decimal 'accumulator
Dim DecGPATotal As Decimal
'determine if txtGender is M or F and if txtGPA is numeric
If Me.txtGender.Text.ToUpper() = "M" Or Me.txtGender.Text.ToUpper() = "F" Then
'assign input values to variables
decGPA = Convert.ToDecimal(Me.txtGPA.Text)
strGender = Me.txtGender.Text.ToUpper()
'determine if decGPA is between 0 and 4
If decGPA >= 0D AndAlso decGPA <= 4D Then
If "M" = strGender Then
'calculate average Male GPA
intNumMale = intNumMale + 1
decGPAMaleTotal = decGPAMaleTotal + decGPA
decGPAMale = decGPAMaleTotal / intNumMale
Else
'calculate average Female GPA
intNumFemale = intNumFemale + 1
decGPAFemaleTotal = decGPAFemaleTotal + decGPA
decGPAFemale = decGPAFemaleTotal / intNumFemale
End If
'calculate average total GPA
DecGPATotal = (decGPAMale + decGPAFemale) / 2
'display GPA's
Me.lblMale.Text = decGPAMale.ToString("F2")
Me.lblFemale.Text = decGPAFemale.ToString("F2")
Me.lblTotal.Text = DecGPATotal.ToString("F2")
Else
MessageBox.Show("The GPA must be between 0 and 4.", _
"GPA Calculator", MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End If
Else
MessageBox.Show("The gender must be either M or F", _
"GPA Calculator", MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End If
Last edited by Poppeseed; Jun 18th, 2007 at 08:37 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|