7 Replies - 811 Views - Last Post: 11 April 2011 - 06:03 AM Rate Topic: -----

#1 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Trouble formatting and output - beginner is lost

Posted 10 April 2011 - 07:48 AM

I cannot seem to display the hits, average and on-base percentage. Here is what I am trying to do. I've attached a picture of the output.

Create a program to calculate batting statistics.

Hits = singles + doubles + triples + home runs


At bats = plate appearances - sacrifices - base on balls - hit by pitch

Average = hits / at bats and is displayed with three decimals

Total bases = singles + (doubles * 2) + (triples times * 3) + (home runs * 4)

On-base percentage = hits / plate appearances and is displayed with three decimals

Slugging percentage = total bases / at bats and is displayed with three decimals

Use functions and procedures when possible. Your program should have at least one function and one
procedure. Do the proper desk checking to ensure that each calculation is correct.





Public Class frmBattingAverage

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        ' Exit and close program
        Me.Close()

    End Sub

    Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click

        ' Declare variables
        Dim Singles As Double
        Dim Doubles As Double
        Dim Triples As Double
        Dim HomeRuns As Double
        Dim Hits As Double
        Dim PlateAppearances As Double
        Dim Sacrifices As Double
        Dim BaseOnBalls As Double
        Dim HitByPitch As Double
        Dim AtBats As Double
        Dim Average As Double
        Dim TotalBases As Double
        Dim OnBasePercentage As Double
        Dim SluggingPercentage As Double

        ' Receive input and associate with variables
        Singles = Val(txtSingles.Text)
        Doubles = Val(txtDoubles.Text)
        Triples = Val(txtTriples.Text)
        HomeRuns = Val(txtHomeRuns.Text)
        Hits = Val(lblHits.Text)
        PlateAppearances = Val(txtPlate.Text)
        Sacrifices = Val(txtSacrifices.Text)
        BaseOnBalls = Val(txtBaseBalls.Text)
        HitByPitch = Val(txtHitPitch.Text)
        AtBats = Val(lblAtBats.Text)
        Average = Val(lblAverage.Text)
        'OnBasePercentage = Val(lblOnBasePercent.Text)
        ' SluggingPercentage = Val(lblSlugPercent.Text)
        TotalBases = Val(lblTotalBases.Text)

        ' Perform calculations
        AtBats = PlateAppearances - Sacrifices - BaseOnBalls - HitByPitch
        Average = Hits / AtBats ' display with three decimals
        TotalBases = Singles + (Doubles * 2) + (Triples * 3) + (HomeRuns * 4)
        OnBasePercentage = Hits / PlateAppearances  ' display with three decimals
        SluggingPercentage = TotalBases / AtBats ' display with three decimals   

        ' Display results

        lblAtBats.Text = AtBats
        lblAverage.Text = Average
        lblOnBasePercent.Text = FormatPercent(OnBasePercentage, 2, True, True, True)
        lblSlugPercent.Text = FormatPercent(SluggingPercentage, 3, True, True, True)
        lblTotalBases.Text = TotalBases

    End Sub

    Public Function Hits(ByVal HitDoubles As Double, ByVal Singles As Double, ByVal Triples As Double, ByVal Homeruns As Double) As Double
        ' Calculate the number of hits
        Return Singles + HitDoubles + Triples + Homeruns
        lblHits.Text = Hits

    End Function
End Class


Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 1
  • +

Replies To: Trouble formatting and output - beginner is lost

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9497
  • View blog
  • Posts: 35,844
  • Joined: 12-June 08

Re: Trouble formatting and output - beginner is lost

Posted 10 April 2011 - 11:12 AM

Quote

I cannot seem to display the hits, average and on-base percentage. Here is what I am trying to do. I've attached a picture of the output.


Elaborate on this. I see you are assigning values to labels, right? Exactly "how" is this not working?

Side note - if you are dividing values always *ALWAYS* verify the divisor is not zero.
Was This Post Helpful? 0
  • +
  • -

#3 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Re: Trouble formatting and output - beginner is lost

Posted 10 April 2011 - 11:47 AM

If I knew I wouldn't have to ask?
Was This Post Helpful? -1
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9497
  • View blog
  • Posts: 35,844
  • Joined: 12-June 08

Re: Trouble formatting and output - beginner is lost

Posted 10 April 2011 - 12:43 PM

M'kay, let me rephrase that. You are assigning values to a label, right? It is pretty straight forward. Since I am not there looking at your entire solution I need more information. With how your code is currently working - what is it doing? Is it giving you an error? Showing a number that's not expected? Not showing a number what so ever? Throwing the word "bananas" in it? On the flip side what do you *want* it to do? Do you have an example of how you want your output to look like?

I am trying to help, but saying "it's not working" is not going to help me help you out, right? Work with me here and we can see where the problem is.
Was This Post Helpful? 1
  • +
  • -

#5 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Re: Trouble formatting and output - beginner is lost

Posted 10 April 2011 - 10:28 PM

I get a zero value in the labels for hits, average and on-base percentage.

Modi, I apologize for sounding sarcastic. My mom was put in the hospital today and I have been upset all day. Please accept my apologies.

I had imported the project from Visual Studio 2008 to VB Express 2010 but it seemed to import ok.

I redid the code a little. The weird thing is that I do not get a single error message. I wonder if its a logic error or if the import has anything to do with it.

Here is the code:
Public Class frmBattingAverage

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        ' Exit and close program
        Me.Close()

    End Sub

    Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click

        ' Declare variables
        Dim Singles As Double
        Dim Doubles As Double
        Dim Triples As Double
        Dim HomeRuns As Double
        Dim Hits As Double
        Dim PlateAppearances As Double
        Dim Sacrifices As Double
        Dim BaseOnBalls As Double
        Dim HitByPitch As Double
        Dim AtBats As Double
        Dim Average As Double
        Dim TotalBases As Double
        Dim OnBasePercentage As Double
        Dim SluggingPercentage As Double

        ' Receive input and associate with variables
        Singles = Val(txtSingles.Text)
        Doubles = Val(txtDoubles.Text)
        Triples = Val(txtTriples.Text)
        HomeRuns = Val(txtHomeRuns.Text)
        Hits = Val(lblHits.Text)
        PlateAppearances = Val(txtPlate.Text)
        Sacrifices = Val(txtSacrifices.Text)
        BaseOnBalls = Val(txtBaseBalls.Text)
        HitByPitch = Val(txtHitPitch.Text)
        AtBats = Val(lblAtBats.Text)
        Average = Val(lblAverage.Text)
        OnBasePercentage = Val(lblOnBasePercent.Text)
        SluggingPercentage = Val(lblSlugPercent.Text)
        TotalBases = Val(lblTotalBases.Text)

        ' Perform calculations
        AtBats = PlateAppearances - Sacrifices - BaseOnBalls - HitByPitch
        Average = Hits / AtBats ' display with three decimals
        TotalBases = Singles + (Doubles * 2) + (Triples * 3) + (HomeRuns * 4)
        OnBasePercentage = Hits / PlateAppearances  ' display with three decimals
        SluggingPercentage = TotalBases / AtBats ' display with three decimals   

        ' Display results
        lblHits.Text = Hits
        lblAtBats.Text = AtBats
        lblAverage.Text = Average
        lblOnBasePercent.Text = FormatPercent(OnBasePercentage, 2, True, True, True)
        lblSlugPercent.Text = FormatPercent(SluggingPercentage, 3, True, True, True)
        lblTotalBases.Text = TotalBases

    End Sub

    Public Function Hits(ByVal Singles As Double, ByVal Doubles As Double, ByVal Triples As Double, ByVal Homeruns As Double) As Double
        ' Calculate the number of hits
        Return Singles + Doubles + Triples + Homeruns
        'lblHits.Text = Hits

    End Function
End Class

Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Trouble formatting and output - beginner is lost

Posted 11 April 2011 - 12:43 AM

CodeWomanSRQ1234 the following blog post will be of use to you.

Textboxes and Numbers (Are these Numbers?)
Was This Post Helpful? 1
  • +
  • -

#7 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Re: Trouble formatting and output - beginner is lost

Posted 11 April 2011 - 04:58 AM

I wish I could find a basic tutorial for idiots. lol. My book is stupid. They just bring u through a code example and do not explain concepts with a lot of different programs and examples as well as more explanations. Like I only found only 1 page of information on the Check Box control, for example.

I give up. Thanks for helping anyway.
Was This Post Helpful? 1
  • +
  • -

#8 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Re: Trouble formatting and output - beginner is lost

Posted 11 April 2011 - 06:03 AM

I got it. Finally!!!
Public Class frmBattingAverage

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        ' Exit and close program
        Me.Close()

    End Sub

    Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click

        ' Declare variables
        Dim Singles As Double
        Dim Doubles As Double
        Dim Triples As Double
        Dim HomeRuns As Double
        Dim Hits As Double
        Dim PlateAppearances As Double
        Dim Sacrifices As Double
        Dim BaseOnBalls As Double
        Dim HitByPitch As Double
        Dim AtBats As Double
        Dim Average As Double
        Dim TotalBases As Double
        Dim OnBasePercentage As Double
        Dim SluggingPercentage As Double

        ' Receive input and associate with variables
        Singles = Val(txtSingles.Text)
        Doubles = Val(txtDoubles.Text)
        Triples = Val(txtTriples.Text)
        HomeRuns = Val(txtHomeRuns.Text)
        PlateAppearances = Val(txtPlate.Text)
        Sacrifices = Val(txtSacrifices.Text)
        BaseOnBalls = Val(txtBaseBalls.Text)
        HitByPitch = Val(txtHitPitch.Text)

        ' Perform calculations and display results
        Average = Hits / AtBats ' display with three decimals
        lblHits.Text = Val(txtSingles.Text) + Val(txtDoubles.Text) + Val(txtTriples.Text) + Val(txtHomeRuns.Text)
        lblAtBats.Text = Val(txtPlate.Text) - Val(txtSacrifices.Text) - Val(txtBaseBalls.Text) - Val(txtHitPitch.Text)
        Average = Val(lblHits.Text) / Val(lblAtBats.Text)
        lblAverage.Text = FormatPercent(Average, 2, True, True, True)
        OnBasePercentage = Val(lblHits.Text) / Val(txtPlate.Text)  ' display with two decimals
        lblOnBasePercent.Text = FormatPercent(OnBasePercentage, 2, True, True, True)
        TotalBases = Singles + (Doubles * 2) + (Triples * 3) + (HomeRuns * 4)
        lblTotalBases.Text = TotalBases
        SluggingPercentage = Val(lblTotalBases.Text) / Val(lblAtBats.Text) ' display with three decimals   
        lblSlugPercent.Text = FormatPercent(SluggingPercentage, 3, True, True, True)
        

    End Sub

    Public Function Hits(ByVal Singles As Double, ByVal Doubles As Double, ByVal Triples As Double, ByVal Homeruns As Double) As Double
        ' Calculate the number of hits
        Return Singles + Doubles + Triples + Homeruns


    End Function
End Class

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1