|
-
December 1st, 2010, 04:05 PM
#1
Help with list and loop?
New to programming and looking for some help. I need this program to allow me to enter 12 temperatures and give me an average. I have everything working correctly ( I think) except where the average should be I get 0.0.
Not sure if it matters but I'm using VB 2005.
Code:
Option Strict On
Public Class frmBoard
Private Sub frmBoard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Dim strTemperature As String
Dim decTemperature As Decimal
Dim decAverageTemperature As Decimal
Dim decTotalOfAllTemperatures As Decimal = 0D
Dim strInputBoxMessage As String = "Enter the temperature for the month #"
Dim strInputBoxHeading As String = "Monthly Temperature"
Dim strNormalBoxMessage As String = "Enter the temperature for the month #"
Dim strNonNumericErrorMessagae As String = "Error - Enter a number for the temperature"
Dim strNegativeNumberErrorMessage As String = "Error - Enter a postive number for the temperature"
' Declare and initialize loop variables
Dim strCancelButtonClicked As String = ""
Dim intMaximumNumberOfEntries As Integer = 12
Dim intNumberOfEntries As Integer = 1
' This loop allows the user to enter the temperature of up to 12 months.
' The loop terminates when the user has entered 12 temperatures or the user
' clicks the Cancel button or the Close button in the InputBox
strTemperature = InputBox(strInputBoxMessage _
& intNumberOfEntries, strInputBoxHeading, " ")
Do Until intNumberOfEntries > intMaximumNumberOfEntries _
Or strTemperature = strCancelButtonClicked
If IsNumeric(strTemperature) Then
decTemperature = Convert.ToDecimal(strTemperature)
If decTemperature > 0 Then
Me.lstTemp.Items.Add(decTemperature)
decTotalOfAllTemperatures += decTemperature
intNumberOfEntries += 1
strInputBoxMessage = strNormalBoxMessage
Else
strInputBoxMessage = strNegativeNumberErrorMessage
End If
Else
strInputBoxMessage = strNonNumericErrorMessagae
End If
If intNumberOfEntries <= intMaximumNumberOfEntries Then
strTemperature = InputBox(strInputBoxMessage _
& intNumberOfEntries, strInputBoxHeading, " ")
End If
Loop
' Makes Label Visible
Me.lblAverageTemperature.Visible = True
' Calculates and displays average temperature
If intNumberOfEntries > 1 Then
decTemperature = decTotalOfAllTemperatures / (intNumberOfEntries - 1)
Me.lblAverageTemperature.Text = "Average Temperature is " & _
decAverageTemperature.ToString("F1") & " degrees"
Else
Me.lblAverageTemperature.Text = "No Temperature Entered"
End If
' Disables the Enter Temperature button
Me.btnEnter.Enabled = False
End Sub
Private Sub mnuClearItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClearItem.Click
Me.lstTemp.Items.Clear()
Me.lblAverageTemperature.Visible = False
Me.btnEnter.Enabled = True
End Sub
Private Sub mnuExitItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExitItem.Click
Me.Close()
End Sub
End Class
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
|