views:

1319

answers:

6

Huge noob here trying to teach himself VB. I need to compute the percentage of games won and games lost. I keep getting errors and it's driving me up a wall. I've changed my code so many times that I don't really remember what it was like when I started. Any help would be greatly appreciated, Thanks in advance.

Private Sub btnPercentage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercentage.Click
    Dim team As String
    Dim won, lost, percentage As Integer

    team = CStr(txtTeam.Text)
    won = CInt(txtGamesWon.Text)
    lost = CInt(txtGamesLost.Text)
    percentage = CInt(txtPercent.Text)

    percentage = (won + lost) / 2

    txtPercent.Text = team & " won" & percentage & " of its games."
+2  A: 

Percentage won = total won / (total won + total lost)

You may have to multiply that by 100 for display purposes.

BoltBait
+1. And remove the first "percentage = ..." line, it's superfluous.
paxdiablo
Definition of percentage includes the '100' factor otherwise it is called ratio.
J.F. Sebastian
+1  A: 

percentage = (won + lost) / 2 can't be right.

Isn't the percentage won percentage_won = won / (won + lost).

And the percentage lost percentage_lost = lost / (won + lost).

S.Lott
+6  A: 

Think the problem through in your head first before you start writing the code. Right now this is the way your code is working:

"Team A has won 5 games and lost 3, so the percentage of games it has won is (5 + 3) / 2 = 4%"

Probably the single most important skill as a programmer is the ability to break problems down into steps for the computer to perform, make sure you've figured out the right steps before you start writing them.

Chad Birch
A: 
percentage = won / (won + lost)

BTW, if Visual Studio doesn't barf on:

Dim won, lost, percentage As Integer

It will dim won and lost as objects, not integers.

Jekke
No, it will declare them as Integer.
ggf31416
Just remember that percentage is not an integer. Percentage is a floating point number with a range of 0 to 1. So, there's another problem in the original code. ;)
BoltBait
I keep getting this Error 1 Option Strict On disallows implicit conversions from 'Double' to 'Integer
Davey
@davey: Then post a new question after search the site.
Geoffrey Chetwood
Normal division between Integers result in Doubles. Declare Percentage as a Double and then round it with Math.Round or convert the results into an Integer.
ggf31416
+2  A: 
Dim team as String = txtTeam.Text  
Dim won as Integer = Cint(txtGamesWon.Text)  
Dim lost as Integer = CInt(txtGamesLost.Text)  
Dim percentage as Integer = Cint(won / (won + lost) * 100)  
txtPercent.Text = team & " won " & percentage & "% of its games."

If you want to make it more resistent to bad inputs you could use Integer.TryParse rather than Cint.

ggf31416
This has been a great help. You guys rock.
Davey
A: 

Most of your problem here isn't even programming related.

It would help to know the simple math behind calculating a percentage first. This one should have been relatively simple to work out on paper first.

My eyes started bleeding when I read:

percentage = (won + lost) / 2.

Percentage should not be equal to half the number of games played (although many a MLB team would dig having that record).

RKitson